{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import xgboost as xgb\n",
    "import lightgbm as lgb\n",
    "from functools import reduce\n",
    "import datetime\n",
    "import os\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def ftrans(num):\n",
    "    num = int(num)\n",
    "    if num == 0:\n",
    "        return 0\n",
    "    if num == 1000:\n",
    "        return 1\n",
    "    if num == 1500:\n",
    "        return 2\n",
    "    if num == 2000:\n",
    "        return 3\n",
    "\n",
    "def trans(num):\n",
    "    num = int(num)\n",
    "    if num == 0:\n",
    "        return 0\n",
    "    if num == 1:\n",
    "        return 1000\n",
    "    if num == 2:\n",
    "        return 1500\n",
    "    if num == 3:\n",
    "        return 2000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# train_test\n",
    "train = pd.read_table('data/train/subsidy_train.txt',sep=',',header=-1)\n",
    "train.columns = ['id','money']\n",
    "test = pd.read_table('data/test/subsidy_final_test.txt',sep=',',header=-1)\n",
    "test.columns = ['id']\n",
    "test['money'] = np.nan\n",
    "train_test = pd.concat([train,test])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "score_train = pd.read_table('data/train/score_train.txt',sep=',',header=-1)\n",
    "score_train.columns = ['id','college','score']\n",
    "score_test = pd.read_table('data/test/score_final_test.txt',sep=',',header=-1)\n",
    "score_test.columns = ['id','college','score']\n",
    "score_train_test = pd.concat([score_train,score_test])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "college_train_test = score_train_test.groupby([\"id\",\"college\"])[\"score\"].max().unstack([\"college\"]).fillna(0)\n",
    "college_name_list = list(college_train_test.columns)\n",
    "college_train_test = college_train_test.rename(columns=dict(zip(college_name_list,[\"college_\"+str(i) for i in college_name_list]))).reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "college = pd.DataFrame(score_train_test.groupby(['college'])['score'].max()).reset_index()\n",
    "college.columns = ['college','college_num']\n",
    "score_train_test = pd.merge(score_train_test, college, how='left',on='college')\n",
    "score_train_test['college_orderate'] = score_train_test['score']/score_train_test['college_num']\n",
    "del score_train_test[\"college\"]\n",
    "#del score_train_test[\"score\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "score_train_test = pd.merge(score_train_test,college_train_test,how=\"left\",on=\"id\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_test = pd.merge(train_test,score_train_test,how='left',on='id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          id   money   score  college_num  college_orderate  college_9  \\\n",
      "0          0     0.0     1.0       2933.0          0.000341        1.0   \n",
      "1          1     0.0     2.0       2933.0          0.000682        2.0   \n",
      "2          8     0.0  1565.0       1572.0          0.995547        0.0   \n",
      "3          9     0.0  1570.0       1572.0          0.998728        0.0   \n",
      "4         10     0.0     1.0       2301.0          0.000435        0.0   \n",
      "5         11     0.0     2.0       2301.0          0.000869        0.0   \n",
      "6         19     0.0     7.0        963.0          0.007269        0.0   \n",
      "7         20     0.0   356.0        963.0          0.369678        0.0   \n",
      "8         21     0.0   381.0        963.0          0.395639        0.0   \n",
      "9         22  1000.0  1734.0       2830.0          0.612721        0.0   \n",
      "10        23  1500.0  2154.0       2830.0          0.761131        0.0   \n",
      "11        28  1000.0  2245.0       2830.0          0.793286        0.0   \n",
      "12        29     0.0  2247.0       2830.0          0.793993        0.0   \n",
      "13        36     0.0  2269.0       2830.0          0.801767        0.0   \n",
      "14        37     0.0  2272.0       2830.0          0.802827        0.0   \n",
      "15        56     0.0  2365.0       2830.0          0.835689        0.0   \n",
      "16        57     0.0  2376.0       2830.0          0.839576        0.0   \n",
      "17        62     0.0  2433.0       2830.0          0.859717        0.0   \n",
      "18        63     0.0  2442.0       2830.0          0.862898        0.0   \n",
      "19        64  1500.0  2457.0       2830.0          0.868198        0.0   \n",
      "20        65     0.0  2468.0       2830.0          0.872085        0.0   \n",
      "21        66     0.0  2864.0       2933.0          0.976475     2864.0   \n",
      "22        67     0.0  2865.0       2933.0          0.976816     2865.0   \n",
      "23        70     0.0  2872.0       2933.0          0.979202     2872.0   \n",
      "24        71     0.0  2873.0       2933.0          0.979543     2873.0   \n",
      "25        74     0.0  2882.0       2933.0          0.982612     2882.0   \n",
      "26        75     0.0  2885.0       2933.0          0.983635     2885.0   \n",
      "27        76     0.0  2890.0       2933.0          0.985339     2890.0   \n",
      "28        77     0.0  2895.0       2933.0          0.987044     2895.0   \n",
      "29        82     0.0  2906.0       2933.0          0.990794     2906.0   \n",
      "...      ...     ...     ...          ...               ...        ...   \n",
      "21816  32591     NaN     NaN          NaN               NaN        NaN   \n",
      "21817  32592     NaN     NaN          NaN               NaN        NaN   \n",
      "21818  32593     NaN   917.0       2830.0          0.324028        0.0   \n",
      "21819  32594     NaN  1272.0       2933.0          0.433686     1272.0   \n",
      "21820  32595     NaN  2747.0       2830.0          0.970671        0.0   \n",
      "21821  32596     NaN  2108.0       2933.0          0.718718     2108.0   \n",
      "21822  32597     NaN   140.0       2933.0          0.047733      140.0   \n",
      "21823  32598     NaN   219.0        582.0          0.376289        0.0   \n",
      "21824  32599     NaN  1470.0       2306.0          0.637467        0.0   \n",
      "21825  32600     NaN   379.0        963.0          0.393562        0.0   \n",
      "21826  32601     NaN   118.0       2418.0          0.048801        0.0   \n",
      "21827  32604     NaN  1383.0       2933.0          0.471531     1383.0   \n",
      "21828  32605     NaN   215.0       2053.0          0.104725        0.0   \n",
      "21829  32616     NaN    76.0        749.0          0.101469        0.0   \n",
      "21830  32617     NaN    85.0       2053.0          0.041403        0.0   \n",
      "21831  32620     NaN  1867.0       2933.0          0.636550     1867.0   \n",
      "21832  32621     NaN  1736.0       2782.0          0.624012        0.0   \n",
      "21833  32628     NaN  1677.0       2782.0          0.602804        0.0   \n",
      "21834  32629     NaN  1047.0       2782.0          0.376348        0.0   \n",
      "21835  32632     NaN  1298.0       1572.0          0.825700        0.0   \n",
      "21836  32633     NaN    84.0       2306.0          0.036427        0.0   \n",
      "21837  32634     NaN  1232.0       2933.0          0.420048     1232.0   \n",
      "21838  32635     NaN     NaN          NaN               NaN        NaN   \n",
      "21839  32636     NaN  2100.0       2301.0          0.912647        0.0   \n",
      "21840  32637     NaN  1055.0       2830.0          0.372792        0.0   \n",
      "21841  32651     NaN  2642.0       2782.0          0.949676        0.0   \n",
      "21842  32660     NaN   749.0        749.0          1.000000        0.0   \n",
      "21843  32661     NaN   911.0       2782.0          0.327462        0.0   \n",
      "21844  32662     NaN   880.0       2418.0          0.363937        0.0   \n",
      "21845  32663     NaN     NaN          NaN               NaN        NaN   \n",
      "\n",
      "       college_19  college_6  college_3  college_4     ...      college_13  \\\n",
      "0             0.0        0.0        0.0        0.0     ...             0.0   \n",
      "1             0.0        0.0        0.0        0.0     ...             0.0   \n",
      "2             0.0     1565.0        0.0        0.0     ...             0.0   \n",
      "3             0.0     1570.0        0.0        0.0     ...             0.0   \n",
      "4             0.0        0.0        1.0        0.0     ...             0.0   \n",
      "5             0.0        0.0        2.0        0.0     ...             0.0   \n",
      "6             0.0        0.0        0.0        0.0     ...             0.0   \n",
      "7             0.0        0.0        0.0        0.0     ...             0.0   \n",
      "8             0.0        0.0        0.0        0.0     ...             0.0   \n",
      "9             0.0        0.0        0.0        0.0     ...             0.0   \n",
      "10            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "11            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "12            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "13            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "14            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "15            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "16            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "17            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "18            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "19            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "20            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "22            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "23            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "24            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "25            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "26            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "27            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "28            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "29            0.0        0.0        0.0        0.0     ...             0.0   \n",
      "...           ...        ...        ...        ...     ...             ...   \n",
      "21816         NaN        NaN        NaN        NaN     ...             NaN   \n",
      "21817         NaN        NaN        NaN        NaN     ...             NaN   \n",
      "21818         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21819         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21820         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21821         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21822         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21823         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21824      1470.0        0.0        0.0        0.0     ...             0.0   \n",
      "21825         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21826         0.0        0.0        0.0      118.0     ...             0.0   \n",
      "21827         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21828         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21829         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21830         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21831         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21832         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21833         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21834         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21835         0.0     1298.0        0.0        0.0     ...             0.0   \n",
      "21836        84.0        0.0        0.0        0.0     ...             0.0   \n",
      "21837         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21838         NaN        NaN        NaN        NaN     ...             NaN   \n",
      "21839         0.0        0.0     2100.0        0.0     ...             0.0   \n",
      "21840         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21841         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21842         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21843         0.0        0.0        0.0        0.0     ...             0.0   \n",
      "21844         0.0        0.0        0.0      880.0     ...             0.0   \n",
      "21845         NaN        NaN        NaN        NaN     ...             NaN   \n",
      "\n",
      "       college_12  college_17  college_15  college_11  college_16  college_1  \\\n",
      "0             0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "1             0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "2             0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "3             0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "4             0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "5             0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "6             0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "7             0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "8             0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "9             0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "10            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "11            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "12            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "13            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "14            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "15            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "16            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "17            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "18            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "19            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "20            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "22            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "23            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "24            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "25            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "26            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "27            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "28            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "29            0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "...           ...         ...         ...         ...         ...        ...   \n",
      "21816         NaN         NaN         NaN         NaN         NaN        NaN   \n",
      "21817         NaN         NaN         NaN         NaN         NaN        NaN   \n",
      "21818         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21819         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21820         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21821         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21822         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21823         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21824         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21825         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21826         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21827         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21828         0.0       215.0         0.0         0.0         0.0        0.0   \n",
      "21829         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21830         0.0        85.0         0.0         0.0         0.0        0.0   \n",
      "21831         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21832         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21833         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21834         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21835         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21836         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21837         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21838         NaN         NaN         NaN         NaN         NaN        NaN   \n",
      "21839         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21840         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21841         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21842         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21843         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21844         0.0         0.0         0.0         0.0         0.0        0.0   \n",
      "21845         NaN         NaN         NaN         NaN         NaN        NaN   \n",
      "\n",
      "       college_10  college_2  college_18  \n",
      "0             0.0        0.0         0.0  \n",
      "1             0.0        0.0         0.0  \n",
      "2             0.0        0.0         0.0  \n",
      "3             0.0        0.0         0.0  \n",
      "4             0.0        0.0         0.0  \n",
      "5             0.0        0.0         0.0  \n",
      "6             0.0        0.0         0.0  \n",
      "7             0.0        0.0         0.0  \n",
      "8             0.0        0.0         0.0  \n",
      "9             0.0        0.0         0.0  \n",
      "10            0.0        0.0         0.0  \n",
      "11            0.0        0.0         0.0  \n",
      "12            0.0        0.0         0.0  \n",
      "13            0.0        0.0         0.0  \n",
      "14            0.0        0.0         0.0  \n",
      "15            0.0        0.0         0.0  \n",
      "16            0.0        0.0         0.0  \n",
      "17            0.0        0.0         0.0  \n",
      "18            0.0        0.0         0.0  \n",
      "19            0.0        0.0         0.0  \n",
      "20            0.0        0.0         0.0  \n",
      "21            0.0        0.0         0.0  \n",
      "22            0.0        0.0         0.0  \n",
      "23            0.0        0.0         0.0  \n",
      "24            0.0        0.0         0.0  \n",
      "25            0.0        0.0         0.0  \n",
      "26            0.0        0.0         0.0  \n",
      "27            0.0        0.0         0.0  \n",
      "28            0.0        0.0         0.0  \n",
      "29            0.0        0.0         0.0  \n",
      "...           ...        ...         ...  \n",
      "21816         NaN        NaN         NaN  \n",
      "21817         NaN        NaN         NaN  \n",
      "21818         0.0        0.0         0.0  \n",
      "21819         0.0        0.0         0.0  \n",
      "21820         0.0        0.0         0.0  \n",
      "21821         0.0        0.0         0.0  \n",
      "21822         0.0        0.0         0.0  \n",
      "21823         0.0        0.0         0.0  \n",
      "21824         0.0        0.0         0.0  \n",
      "21825         0.0        0.0         0.0  \n",
      "21826         0.0        0.0         0.0  \n",
      "21827         0.0        0.0         0.0  \n",
      "21828         0.0        0.0         0.0  \n",
      "21829        76.0        0.0         0.0  \n",
      "21830         0.0        0.0         0.0  \n",
      "21831         0.0        0.0         0.0  \n",
      "21832         0.0        0.0         0.0  \n",
      "21833         0.0        0.0         0.0  \n",
      "21834         0.0        0.0         0.0  \n",
      "21835         0.0        0.0         0.0  \n",
      "21836         0.0        0.0         0.0  \n",
      "21837         0.0        0.0         0.0  \n",
      "21838         NaN        NaN         NaN  \n",
      "21839         0.0        0.0         0.0  \n",
      "21840         0.0        0.0         0.0  \n",
      "21841         0.0        0.0         0.0  \n",
      "21842       749.0        0.0         0.0  \n",
      "21843         0.0        0.0         0.0  \n",
      "21844         0.0        0.0         0.0  \n",
      "21845         NaN        NaN         NaN  \n",
      "\n",
      "[21846 rows x 24 columns]\n"
     ]
    }
   ],
   "source": [
    "print train_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "card_train = pd.read_table('data/train/card_train.txt',sep=',',header=-1)\n",
    "card_train.columns = ['id','consume','where','how','time','amount','remainder']\n",
    "card_test = pd.read_table('data/test/card_final_test.txt',sep=',',header=-1)\n",
    "card_test.columns = ['id','consume','where','how','time','amount','remainder']\n",
    "card_train_test = pd.concat([card_train,card_test])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             id consume   where  how                 time  amount  remainder\n",
      "0          1006   POS消费   地点551   淋浴  2013/09/01 00:00:32    0.50     124.90\n",
      "1          1006   POS消费   地点551   淋浴  2013/09/01 00:00:32    0.50     124.90\n",
      "2          1968   POS消费   地点159   淋浴  2013/09/01 00:00:39    0.10     200.14\n",
      "3          1968   POS消费   地点159   淋浴  2013/09/01 00:00:39    0.10     200.14\n",
      "4          1406   POS消费   地点660   开水  2013/09/01 00:00:40    0.01     374.42\n",
      "5          1406   POS消费   地点660   开水  2013/09/01 00:00:40    0.01     374.42\n",
      "6          1406   POS消费    地点78   其他  2013/09/01 00:00:40    0.60     373.82\n",
      "7          1406   POS消费    地点78   其他  2013/09/01 00:00:40    0.60     373.82\n",
      "8         13554   POS消费     地点6   淋浴  2013/09/01 00:00:57    0.50     522.37\n",
      "9         13554   POS消费     地点6   淋浴  2013/09/01 00:00:57    0.50     522.37\n",
      "10         5582   POS消费    地点30   开水  2013/09/01 00:01:01    0.27     512.04\n",
      "11         5582   POS消费    地点30   开水  2013/09/01 00:01:01    0.27     512.04\n",
      "12         4784   POS消费    地点31   淋浴  2013/09/01 00:01:14    0.90      60.59\n",
      "13         4784   POS消费    地点31   淋浴  2013/09/01 00:01:14    0.90      60.59\n",
      "14        12908   POS消费   地点590   淋浴  2013/09/01 00:01:20    0.40      95.70\n",
      "15        12908   POS消费   地点590   淋浴  2013/09/01 00:01:20    0.40      95.70\n",
      "16         5008   POS消费    地点24   淋浴  2013/09/01 00:02:29    1.00     306.93\n",
      "17         5008   POS消费    地点24   淋浴  2013/09/01 00:02:29    1.00     306.93\n",
      "18         1040   POS消费   地点661  洗衣房  2013/09/01 00:02:40    1.20     200.71\n",
      "19         1040   POS消费   地点661  洗衣房  2013/09/01 00:02:40   -0.48     201.19\n",
      "20         1040   POS消费   地点661  洗衣房  2013/09/01 00:02:40   -0.48     201.19\n",
      "21         1040   POS消费   地点661  洗衣房  2013/09/01 00:02:40    1.20     200.71\n",
      "22        21622   POS消费   地点117   开水  2013/09/01 00:02:56    0.02     454.60\n",
      "23        21622   POS消费   地点117   开水  2013/09/01 00:02:56    0.02     454.60\n",
      "24        15708   POS消费     地点6   淋浴  2013/09/01 00:03:03    0.10      30.11\n",
      "25        15708   POS消费     地点6   淋浴  2013/09/01 00:03:03    0.10      30.11\n",
      "26         3912   POS消费   地点139   开水  2013/09/01 00:03:07    0.02     200.34\n",
      "27         3912   POS消费   地点139   开水  2013/09/01 00:03:07    0.02     200.34\n",
      "28        19796   POS消费   地点602   淋浴  2013/09/01 00:03:15    0.60     256.50\n",
      "29        19796   POS消费   地点602   淋浴  2013/09/01 00:03:15    0.60     256.50\n",
      "...         ...     ...     ...  ...                  ...     ...        ...\n",
      "12657143  22451   POS消费   地点979   淋浴  2015/08/31 23:56:15    0.70       8.94\n",
      "12657144  31970   POS消费   地点879   淋浴  2015/08/31 23:56:19    0.10      64.15\n",
      "12657145  17786   POS消费   地点473   淋浴  2015/08/31 23:56:21    0.70      76.85\n",
      "12657146  31713   POS消费   地点667   淋浴  2015/08/31 23:56:23    0.40      58.32\n",
      "12657147  24066   POS消费  地点2006   淋浴  2015/08/31 23:56:23    0.70     332.08\n",
      "12657148  14396   POS消费    地点34   淋浴  2015/08/31 23:56:26    0.80       4.31\n",
      "12657149  16833   POS消费  地点2364   淋浴  2015/08/31 23:56:30    0.30     174.41\n",
      "12657150  15685   POS消费   地点856   淋浴  2015/08/31 23:56:41    0.50       0.42\n",
      "12657151   4987   POS消费  地点1753   淋浴  2015/08/31 23:57:02    0.90     169.56\n",
      "12657152  24993   POS消费   地点705   淋浴  2015/08/31 23:57:10    0.70     117.91\n",
      "12657153   9444   POS消费   地点947   淋浴  2015/08/31 23:57:18    0.80     145.31\n",
      "12657154  12105   POS消费    地点51  洗衣房  2015/08/31 23:57:19   -2.52     144.16\n",
      "12657155  12105   POS消费    地点51  洗衣房  2015/08/31 23:57:19    3.60     141.64\n",
      "12657156  29928   POS消费    地点10   淋浴  2015/08/31 23:57:22    0.10      28.86\n",
      "12657157  31970   POS消费    地点43   淋浴  2015/08/31 23:57:25    0.20      64.25\n",
      "12657158  13620   POS消费   地点742   淋浴  2015/08/31 23:57:25    0.40      81.60\n",
      "12657159   2151   POS消费  地点1419   淋浴  2015/08/31 23:57:28    0.50      17.85\n",
      "12657160  10567   POS消费    地点53   淋浴  2015/08/31 23:57:32    0.90      45.71\n",
      "12657161  31713   POS消费   地点667   淋浴  2015/08/31 23:57:34    0.20      58.12\n",
      "12657162   4791   POS消费   地点159   淋浴  2015/08/31 23:57:57    0.50      84.09\n",
      "12657163  19872   POS消费   地点954   淋浴  2015/08/31 23:58:22    0.40     192.64\n",
      "12657164  11321   POS消费   地点125   淋浴  2015/08/31 23:58:35    1.10     146.40\n",
      "12657165    350   POS消费   地点902   淋浴  2015/08/31 23:58:44    0.20     167.42\n",
      "12657166  29928   POS消费    地点10   淋浴  2015/08/31 23:59:23    0.10      28.66\n",
      "12657167  29928   POS消费    地点10   淋浴  2015/08/31 23:59:23    0.10      28.76\n",
      "12657168  20398   POS消费  地点1655   淋浴  2015/08/31 23:59:25    0.20      56.10\n",
      "12657169  16901   POS消费   地点741   淋浴  2015/08/31 23:59:26    1.20      16.59\n",
      "12657170   9121   POS消费  地点2297   淋浴  2015/08/31 23:59:27    0.10      32.06\n",
      "12657171  21244   POS消费   地点739   淋浴  2015/08/31 23:59:55    0.70     445.33\n",
      "12657172   6539   POS消费  地点2454   淋浴  2015/08/31 23:59:59    0.50      45.52\n",
      "\n",
      "[25112731 rows x 7 columns]\n"
     ]
    }
   ],
   "source": [
    "print card_train_test\n",
    "#print card_train_test.groupby(\"where\")[\"how\"].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "card = pd.DataFrame(card_train_test.groupby(['id'])[\"time\"].count()).reset_index().rename(columns={\"time\":\"cardActionNum\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\"剩余金额上，“和”这个概念是不应该的，因此次数越多和就越大，所以一个人的和越大，不一定是体现出消费能力，可能只是比别人多几次消费\"\"\"\n",
    "\"\"\"用剩余金额的均值、众数、最小值和最大值还是有一定的理论意义的\"\"\"\n",
    "\"\"\"并且剩余金额这个特征是不需要依附其他特征而存在的，本身就可以比较\"\"\"\n",
    "#card['remaindersum'] = card_train_test.groupby(['id'])['remainder'].sum()\n",
    "card['remainderavg'] = card_train_test.groupby(['id'])['remainder'].mean()\n",
    "card['remaindermax'] = card_train_test.groupby(['id'])['remainder'].max()\n",
    "card['remaindermin'] = card_train_test.groupby(['id'])['remainder'].min()\n",
    "card['remaindermedian'] = card_train_test.groupby(['id'])['remainder'].median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "card_train_test['time'] = card_train_test['time'].apply(lambda x: datetime.datetime.strptime(\n",
    "        x, \"%Y/%m/%d %H:%M:%S\").date())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "card_how_count = card_train_test.groupby([\"id\",\"how\"])[\"amount\"].count().unstack([\"how\"])\n",
    "card_how_count = card_how_count.rename(columns=dict(zip(card_how_count,[str(i)+\"_count\" for i in card_how_count])))\n",
    "\n",
    "card_how_sum = card_train_test.groupby([\"id\",\"how\"])[\"amount\"].sum().unstack([\"how\"])\n",
    "card_how_sum = card_how_sum.rename(columns=dict(zip(card_how_sum,[str(i)+\"_sum\" for i in card_how_sum])))\n",
    "\n",
    "card_how_mean = card_train_test.groupby([\"id\",\"how\"])[\"amount\"].mean().unstack([\"how\"])\n",
    "card_how_mean = card_how_mean.rename(columns=dict(zip(card_how_mean,[str(i)+\"_mean\" for i in card_how_mean])))\n",
    "\n",
    "card_how_max = card_train_test.groupby([\"id\",\"how\"])[\"amount\"].max().unstack([\"how\"])\n",
    "card_how_max = card_how_max.rename(columns=dict(zip(card_how_max,[str(i)+\"_max\" for i in card_how_max])))\n",
    "\n",
    "card_how_min = card_train_test.groupby([\"id\",\"how\"])[\"amount\"].min().unstack([\"how\"])\n",
    "card_how_min = card_how_min.rename(columns=dict(zip(card_how_min,[str(i)+\"_min\" for i in card_how_min])))\n",
    "\n",
    "card_how_median = card_train_test.groupby([\"id\",\"how\"])[\"amount\"].median().unstack([\"how\"])\n",
    "card_how_median = card_how_median.rename(columns=dict(zip(card_how_median,[str(i)+\"_median\" for i in card_how_median])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "card_consume_count = card_train_test.groupby([\"id\",\"consume\"])[\"amount\"].count().unstack([\"consume\"])\n",
    "card_consume_count = card_consume_count.rename(columns=dict(zip(card_consume_count,[str(i)+\"_count\" for i in card_consume_count])))\n",
    "\n",
    "card_consume_sum = card_train_test.groupby([\"id\",\"consume\"])[\"amount\"].sum().unstack([\"consume\"])\n",
    "card_consume_sum = card_consume_sum.rename(columns=dict(zip(card_consume_sum,[str(i)+\"_sum\" for i in card_consume_sum])))\n",
    "\n",
    "card_consume_mean = card_train_test.groupby([\"id\",\"consume\"])[\"amount\"].mean().unstack([\"consume\"])\n",
    "card_consume_mean = card_consume_mean.rename(columns=dict(zip(card_consume_mean,[str(i)+\"_mean\" for i in card_consume_mean])))\n",
    "\n",
    "card_consume_max = card_train_test.groupby([\"id\",\"consume\"])[\"amount\"].max().unstack([\"consume\"])\n",
    "card_consume_max = card_consume_max.rename(columns=dict(zip(card_consume_max,[str(i)+\"_max\" for i in card_consume_max])))\n",
    "\n",
    "card_consume_min = card_train_test.groupby([\"id\",\"consume\"])[\"amount\"].min().unstack([\"consume\"])\n",
    "card_consume_min = card_consume_min.rename(columns=dict(zip(card_consume_min,[str(i)+\"_min\" for i in card_consume_min])))\n",
    "\n",
    "card_consume_median = card_train_test.groupby([\"id\",\"consume\"])[\"amount\"].median().unstack([\"consume\"])\n",
    "card_consume_median = card_consume_median.rename(columns=dict(zip(card_consume_median,[str(i)+\"_median\" for i in card_consume_median])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "card_where_count = card_train_test.groupby([\"id\",\"where\"])[\"amount\"].count().unstack([\"where\"])\n",
    "card_where_count = card_where_count.rename(columns=dict(zip(card_where_count,[str(i)+\"_count\" for i in card_where_count])))\n",
    "\n",
    "card_where_sum = card_train_test.groupby([\"id\",\"where\"])[\"amount\"].sum().unstack([\"where\"])\n",
    "card_where_sum = card_where_sum.rename(columns=dict(zip(card_where_sum,[str(i)+\"_sum\" for i in card_where_sum])))\n",
    "\n",
    "card_where_mean = card_train_test.groupby([\"id\",\"where\"])[\"amount\"].mean().unstack([\"where\"])\n",
    "card_where_mean = card_where_mean.rename(columns=dict(zip(card_where_mean,[str(i)+\"_mean\" for i in card_where_mean])))\n",
    "\n",
    "card_where_max = card_train_test.groupby([\"id\",\"where\"])[\"amount\"].max().unstack([\"where\"])\n",
    "card_where_max = card_where_max.rename(columns=dict(zip(card_where_max,[str(i)+\"_max\" for i in card_where_max])))\n",
    "\n",
    "card_where_min = card_train_test.groupby([\"id\",\"where\"])[\"amount\"].min().unstack([\"where\"])\n",
    "card_where_min = card_where_min.rename(columns=dict(zip(card_where_min,[str(i)+\"_min\" for i in card_where_min])))\n",
    "\n",
    "card_where_median = card_train_test.groupby([\"id\",\"where\"])[\"amount\"].median().unstack([\"where\"])\n",
    "card_where_median = card_where_median.rename(columns=dict(zip(card_where_median,[str(i)+\"_median\" for i in card_where_median])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "card_time_count = card_train_test.groupby([\"id\",\"time\"])[\"amount\"].count().unstack([\"time\"])\n",
    "card_time_count = card_time_count.rename(columns=dict(zip(card_time_count,[str(i)+\"_count\" for i in card_time_count])))\n",
    "\n",
    "card_time_sum = card_train_test.groupby([\"id\",\"time\"])[\"amount\"].sum().unstack([\"time\"])\n",
    "card_time_sum = card_time_sum.rename(columns=dict(zip(card_time_sum,[str(i)+\"_sum\" for i in card_time_sum])))\n",
    "\n",
    "card_time_mean = card_train_test.groupby([\"id\",\"time\"])[\"amount\"].mean().unstack([\"time\"])\n",
    "card_time_mean = card_time_mean.rename(columns=dict(zip(card_time_mean,[str(i)+\"_mean\" for i in card_time_mean])))\n",
    "\n",
    "card_time_max = card_train_test.groupby([\"id\",\"time\"])[\"amount\"].max().unstack([\"time\"])\n",
    "card_time_max = card_time_max.rename(columns=dict(zip(card_time_max,[str(i)+\"_max\" for i in card_time_max])))\n",
    "\n",
    "card_time_min = card_train_test.groupby([\"id\",\"time\"])[\"amount\"].min().unstack([\"time\"])\n",
    "card_time_min = card_time_min.rename(columns=dict(zip(card_time_min,[str(i)+\"_min\" for i in card_time_min])))\n",
    "\n",
    "card_time_median = card_train_test.groupby([\"id\",\"time\"])[\"amount\"].median().unstack([\"time\"])\n",
    "card_time_median = card_time_median.rename(columns=dict(zip(card_time_median,[str(i)+\"_median\" for i in card_time_median])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "card_consume_amount = pd.concat([card_consume_count,card_consume_sum,card_consume_max,card_consume_min,card_consume_median,card_consume_mean],axis=1).reset_index().fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "card_where_amount = pd.concat([card_where_count,card_where_sum,card_where_max,card_where_min,card_where_median,card_where_mean],axis=1).reset_index().fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "card_time_amount = pd.concat([card_time_count,card_time_sum,card_time_max,card_time_min,card_time_median,card_time_mean],axis=1).reset_index().fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "card_how_amount = pd.concat([card_how_count,card_how_sum,card_how_max,card_how_min,card_how_median,card_how_mean],axis=1).reset_index().fillna(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## borrow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Anaconda\\lib\\site-packages\\ipykernel\\__main__.py:1: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators; you can avoid this warning by specifying engine='python'.\n",
      "  if __name__ == '__main__':\n",
      "C:\\Anaconda\\lib\\site-packages\\ipykernel\\__main__.py:3: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators; you can avoid this warning by specifying engine='python'.\n",
      "  app.launch_new_instance()\n"
     ]
    }
   ],
   "source": [
    "borrow_train = pd.read_table('data/train/borrow_train.txt',sep=',\"',header=None)\n",
    "borrow_train.columns = ['id','borrowTime','bookName','bookNo']\n",
    "borrow_test = pd.read_table('data/test/borrow_final_test.txt',sep=',\"',header=-1)\n",
    "borrow_test.columns = ['id','borrowTime','bookName','bookNo']\n",
    "borrow_train_test = pd.concat([borrow_train,borrow_test])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "borrow = pd.DataFrame(borrow_train_test.groupby(['id'])[\"borrowTime\"].count()).reset_index().rename(columns={\"borrowTime\":\"borrowActionNum\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def borrow_find(str):\n",
    "    flag = 0\n",
    "    if ((str.find(\"数学建模\")!=-1)|(str.find(\"建模\")!=-1)):\n",
    "        str = \"数学建模\"\n",
    "        flag = 1\n",
    "    if (str.find(\"研究生\")!=-1)|(str.find(\"考研\")!=-1):\n",
    "        str = \"考研\"\n",
    "        flag = 1\n",
    "    if ((str.find(\"数学\")!=-1)|(str.find(\"微积分\")!=-1)|(str.find(\"线性代数\")!=-1)|(str.find(\"数值分析\")!=-1)\n",
    "      |(str.find(\"概率论\")!=-1)|(str.find(\"数理\")!=-1)|(str.find(\"复变函数\")!=-1)|(str.find(\"应用统计\")!=-1)|(str.find(\"几何\")!=-1)|(str.find(\"方程\")!=-1)):\n",
    "        str = \"数学\"\n",
    "        flag = 1\n",
    "    if str.find(\"单人研修室\")!=-1:\n",
    "        str = \"单人研修室\"\n",
    "        flag = 1\n",
    "    if str.find(\"三人研修室\")!=-1:\n",
    "        str = \"三人研修室\"\n",
    "        flag = 1\n",
    "    if str.find(\"六人研修室\")!=-1:\n",
    "        str = \"六人研修室\"\n",
    "        flag = 1\n",
    "    if (str.find(\"托福\")!=-1)|(str.find(\"雅思\")!=-1)|(str.find(\"GRE\")!=-1)|(str.find(\"TOEFL\")!=-1)|(str.find(\"耶鲁\")!=-1)|(str.find(\"TOEFL\")!=-1):\n",
    "        str = \"出国\"\n",
    "        flag = 1\n",
    "    if (str.find(\"英语\")!=-1)|(str.find(\"四六级\")!=-1):\n",
    "        str = \"英语\"\n",
    "        flag = 1\n",
    "    if ((str.find(\"Java\")!=-1)|(str.find(\"计算机\")!=-1)|(str.find(\"电子\")!=-1)|(str.find(\"数据结构\")!=-1)|(str.find(\"算法\")!=-1)\n",
    "        |(str.find(\"C语言\")!=-1)|(str.find(\"C++\")!=-1)|(str.find(\"程序设计\")!=-1)|(str.find(\"电路\")!=-1)\n",
    "         |(str.find(\"系统设计\")!=-1)|(str.find(\"图像处理\")!=-1)|(str.find(\"程序设计\")!=-1)|(str.find(\"数据库\")!=-1)\n",
    "        |(str.find(\"编程\")!=-1)|(str.find(\"JavaEE\")!=-1)|(str.find(\"HTML\")!=-1)|(str.find(\"四色问题\")!=-1)\n",
    "       |(str.find(\"Photoshop\")!=-1)|(str.find(\"软件\")!=-1)|(str.find(\"MATLAB\")!=-1)|(str.find(\"SPSS\")!=-1)\n",
    "         |(str.find(\"单片机\")!=-1)|(str.find(\"代码\")!=-1)|(str.find(\"编译\")!=-1)|(str.find(\"传感器\")!=-1)\n",
    "       |(str.find(\"Linux\")!=-1)|(str.find(\"软件\")!=-1)|(str.find(\"信号\")!=-1)|(str.find(\"网络\")!=-1)\n",
    "       |(str.find(\"自动控制\")!=-1)|(str.find(\"JavaScript\")!=-1)|(str.find(\"Android\")!=-1)|(str.find(\"云计算\")!=-1)\n",
    "       |(str.find(\"CSS\")!=-1)|(str.find(\"MFC\")!=-1)|(str.find(\"嵌入式\")!=-1)|(str.find(\"云计算\")!=-1)):\n",
    "        str = \"计算机\"\n",
    "        flag = 1\n",
    "    if str.find(\"物理\")!=-1:\n",
    "        str = \"物理\"\n",
    "        flag = 1\n",
    "    if ((str.find(\"会计\")!=-1)|(str.find(\"经济学\")!=-1)):\n",
    "        str = \"经济学\"\n",
    "        flag = 1\n",
    "    if (str.find(\"哲学\")!=-1)|(str.find(\"菜根谭\")!=-1)|(str.find(\"心理学\")!=-1)|(str.find(\"精神\")!=-1)|(str.find(\"形而上学\")!=-1)|(str.find(\"思维\")!=-1):\n",
    "        str = \"哲学\"\n",
    "        flag = 1\n",
    "    if str.find(\"法语\")!=-1:\n",
    "        str = \"法语\"\n",
    "        flag = 1\n",
    "    if str.find(\"日语\")!=-1:\n",
    "        str = \"日语\"\n",
    "        flag = 1\n",
    "    if str.find(\"公务员\")!=-1:\n",
    "        str = \"公务员考试\"\n",
    "        flag = 1\n",
    "    if flag ==1:\n",
    "        return str\n",
    "    else:\n",
    "        return \"小说\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def borrow_find2(str):\n",
    "    flag = 0\n",
    "    if (str.find(\"小说\")!=-1):\n",
    "        str = \"课外阅读\"\n",
    "        flag = 1\n",
    "    if flag==1:\n",
    "        return str\n",
    "    else:\n",
    "        return \"课内学习\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           id   borrowTime                                           bookName  \\\n",
      "0        9708   2014/2/25\"    我的英语日记 wo de ying yu ri ji / (韩)南银英著  (韩)卢炫廷插图\"   \n",
      "1        6956  2013/10/27\"         解读联想思维 jie du lian xiang si wei : 联想教父柳传志\"   \n",
      "2       23939    2015/3/8\"                         电路分析 dian lu fen xi  刘健主编\"   \n",
      "3       22047  2014/12/29\"  现代物流学 xian dai wu liu xue = Contemporary logis...   \n",
      "4        9076   2014/3/28\"  公司法 gong si fa = = Corporation law / 范健, 王建文著 ...   \n",
      "5        6406   2014/4/13\"                             发烧 fa shao  北村著 张润世绘图\"   \n",
      "6       29333   2015/7/14\"  数字绘画与创意 shu zi hui hua yu chuang yi (美)Cher Th...   \n",
      "7       16266   2014/6/26\"  周汝昌评说四大名著 zhou ru chang ping shuo si da ming z...   \n",
      "8       15150   2014/1/11\"  管理人力资源 guan li ren li zi yuan 合作伙伴的责任、定位与分工 Ma...   \n",
      "9       22653   2015/8/30\"  面朝大海春暖花开 mian chao da hai chun nuan hua kai 海子...   \n",
      "10       6895   2014/11/5\"  通信电子电路学习指导 tong xin dian zi dian lu xue xi zhi...   \n",
      "11      21322   2013/9/30\"  维也纳情感 wei ye na qing gan = Venna musings / 冯骥才...   \n",
      "12      10204    2014/1/2\"                 模拟电子线路 mo ni dian zi xian lu 杨凌编著\"   \n",
      "13       5747    2015/5/3\"     Effective C++, third edition / Scott Meyers著.\"   \n",
      "14       7958  2013/12/12\"  大学生写作指南 da xue sheng xie zuo zhi nan Writing a...   \n",
      "15      19952    2014/3/6\"  图解德鲁克思想精粹 tu jie de lu ke si xiang jing cui = ...   \n",
      "16       3396   2013/11/7\"                       叶落长安 ye lao chang an / 吴文莉著\"   \n",
      "17      25501   2015/1/12\"  老板是怎样炼成的 lao ban shi zen yang lian cheng de / ...   \n",
      "18       1750   2014/2/24\"  做人要懂心理学 做事要讲博弈术 zuo ren yao dong xin li xue  z...   \n",
      "19      16659   2015/6/27\"      报纸编辑学教程 bao zhi bian ji xue jiao cheng 郑兴东等著\"   \n",
      "20      16266    2014/3/3\"  卡波蒂短篇小说全集 ka bo di duan pian xiao shuo quan ji...   \n",
      "21      21334   2014/6/12\"                    儒林外史 ru lin wai shi / (清) 吴敬梓撰\"   \n",
      "22      21180   2013/12/9\"      Java语言程序设计 Java yu yan cheng xu she ji  郎波编著\"   \n",
      "23       7747   2015/1/13\"  像少年啦飞驰 xiang shao nian la fei chi like the win...   \n",
      "24      24829  2014/11/15\"  水孩子 = The water babies / (英) 查尔斯·金斯利原著  王勋，纪飞等...   \n",
      "25      22975    2015/4/6\"  十三封自杀告别信 shi san feng zi sha gao bie xin = Fur...   \n",
      "26      22703   2015/5/14\"  MATLAB实用教程 MATLAB shi yong jiao cheng  徐金明主编 徐...   \n",
      "27      20403    2014/9/1\"  新日本语能力测试问题对策 xin ri ben yu neng li ce shi wen ...   \n",
      "28       7973  2014/11/26\"                   普通话语音 pu tong hua yu yin 金有景[著]\"   \n",
      "29      16733   2014/11/2\"    Java语言程序设计 java yu yan cheng xu she ji / 丁振凡主编\"   \n",
      "...       ...          ...                                                ...   \n",
      "242243  26692   2015/8/31\"  大学英语六级词汇进阶 da xue ying yu liu ji ci hui jin ji...   \n",
      "242244  12377   2015/8/31\"  大学物理学 Da Xue Wu Li Xue 第二册 热学 张三慧,沈慧君编著 Zhang ...   \n",
      "242245   3108   2015/8/31\"  男人一本书 nan ren yi ben shu = Ein mann ein buch /...   \n",
      "242246   4114   2015/8/31\"  新版中日交流标准日本语 xin ban zhong ri jiao liu biao zhu...   \n",
      "242247   4114   2015/8/31\"  新版中日交流标准日本语自学指南 xin ban zhong ri jiao liu biao...   \n",
      "242248   4114   2015/8/31\"  新版中日交流标准日本语同步辅导 xin ban zhong ri jiao liu biao...   \n",
      "242249  18402   2015/8/31\"          热力学·统计物理 re li xue ·tong ji wu li 汪志诚[著]\"   \n",
      "242250  28537   2015/8/31\"    风光摄影精技 feng guang she ying jing ji / 黑瞳, 范晓娟编著\"   \n",
      "242251  28537   2015/8/31\"  Photoshop CS6数码照片处理268例 Photoshop CS6 shu ma z...   \n",
      "242252  16639   2015/8/31\"  中国文化的深层结构 zhong guo wen hua de shen ceng jie g...   \n",
      "242253  16639   2015/8/31\"  VERILOG HDL数字系统设计入门与应用实例 VERILOG HDL shu zi xi...   \n",
      "242254  31424   2015/8/31\"                           趣味逻辑 qu wei luo ji 徐德清著\"   \n",
      "242255  31424   2015/8/31\"  雅思精选试题解析 ya si jing xuan shi ti jie xi  写作 xie...   \n",
      "242256  31424   2015/8/31\"    智断疑案与逻辑推理 zhi duan yi an yu luo ji tui li 吴邛编著\"   \n",
      "242257  31424   2015/8/31\"  悖论简史 bei lun jian shi 哲学和心灵的迷宫 zhe xue he xin ...   \n",
      "242258  24005   2015/8/31\"  考研英语分频词汇高分突破 kao yan ying yu fen pin ci hui ga...   \n",
      "242259  22284   2015/8/31\"  Harry Potter box set, v. 3 : Harry Potter and ...   \n",
      "242260  22284   2015/8/31\"  哈利·波特与阿兹卡班囚徒 ha li· bo te yu a zi ka ban qiu t...   \n",
      "242261  16336   2015/8/31\"    荣格心理术 rong ge xin li shu : 读人善己的6项心里功夫 / 金圣荣编著\"   \n",
      "242262  16336   2015/8/31\"  MATLAB及在电子信息课程中的应用 MATLAB ji zai dian zi xin x...   \n",
      "242263   4114   2015/8/31\"  2007考研英语大纲词汇考点、用法及辨析 2007 kao yan ying yu da g...   \n",
      "242264   4114   2015/8/31\"  2014考研英语时文阅读宝典120+60（夜读） 2014 kao yan ying yu ...   \n",
      "242265   4114   2015/8/31\"  考研英语晨读美文 kao yan ying yu chen du mei wen : 201...   \n",
      "242266   5266   2015/8/31\"               背离技术分析 bei li ji shu fen xi / 江南小隐著\"   \n",
      "242267  12243   2015/8/31\"  TCP/IP网络技术 TCP/IP wang luo ji shu 基于Windows 20...   \n",
      "242268  11952   2015/8/31\"  刀锋 (英)威廉·萨默赛特·毛姆著 dao feng  (ying )wei lian sa...   \n",
      "242269   2640   2015/8/31\"                   信号与系统 xin hao yu xi tong 王明泉等编著\"   \n",
      "242270   2640   2015/8/31\"                   信号与系统 xin hao yu xi tong 王明泉等编著\"   \n",
      "242271   7306   2015/8/31\"  认知心理学 ren zhi xin li xue Cognitive psycholgy (...   \n",
      "242272   7306   2015/8/31\"                  普通心理学 pu tong xin li xue / 游旭群主编\"   \n",
      "\n",
      "                    bookNo bookClassify bookClassify2   bookTimes  \n",
      "0                H315 502\"           英语          课内学习   2014/2/25  \n",
      "1          K825.38=76 547\"           哲学          课内学习  2013/10/27  \n",
      "2             TM133-43 56\"          计算机          课内学习    2015/3/8  \n",
      "3                F252 161\"           小说          课外阅读  2014/12/29  \n",
      "4                     None           小说          课外阅读   2014/3/28  \n",
      "5         I247.57 1101.17\"           小说          课外阅读   2014/4/13  \n",
      "6           TP391.41 T473\"           小说          课外阅读   2015/7/14  \n",
      "7             I207.41 876\"           小说          课外阅读   2014/6/26  \n",
      "8            F272.92 J125\"           小说          课外阅读   2014/1/11  \n",
      "9                I227 393\"           小说          课外阅读   2015/8/30  \n",
      "10      TN710-42 137-2 E2\"          计算机          课内学习   2014/11/5  \n",
      "11             I267 468-7\"           小说          课外阅读   2013/9/30  \n",
      "12           TN710-43 706\"          计算机          课内学习    2014/1/2  \n",
      "13                    None          计算机          课内学习    2015/5/3  \n",
      "14              H315 9288\"           小说          课外阅读  2013/12/12  \n",
      "15                C93 271\"           小说          课外阅读    2014/3/6  \n",
      "16         I247.57 793.45\"           小说          课外阅读   2013/11/7  \n",
      "17             F279.23 49\"           小说          课外阅读   2015/1/12  \n",
      "18          B821-49 525-4\"           哲学          课内学习   2014/2/24  \n",
      "19            G213-43 978\"           小说          课外阅读   2015/6/27  \n",
      "20         I712.45 C177-7\"           小说          课外阅读    2014/3/3  \n",
      "21         I242.47 786-12\"           小说          课外阅读   2014/6/12  \n",
      "22         TP312JA-43 482\"          计算机          课内学习   2013/12/9  \n",
      "23       I247.57 681-5 E2\"           小说          课外阅读   2015/1/13  \n",
      "24        H319.4:I K564.2\"          计算机          课内学习  2014/11/15  \n",
      "25           I516.45 G537\"           小说          课外阅读    2015/4/6  \n",
      "26         TP312MA-43 335\"          计算机          课内学习   2015/5/14  \n",
      "27                    None           小说          课外阅读    2014/9/1  \n",
      "28             H116 915-2\"           小说          课外阅读  2014/11/26  \n",
      "29            TP312JA 115\"          计算机          课内学习   2014/11/2  \n",
      "...                    ...          ...           ...         ...  \n",
      "242243           H313 113\"           英语          课内学习   2015/8/31  \n",
      "242244                None           物理          课内学习   2015/8/31  \n",
      "242245       B825-49 A949\"           小说          课外阅读   2015/8/31  \n",
      "242246   H36 7588 V1:1 E2\"           小说          课外阅读   2015/8/31  \n",
      "242247      H36 365 V1 E3\"           小说          课外阅读   2015/8/31  \n",
      "242248         H36-42 786\"           小说          课外阅读   2015/8/31  \n",
      "242249     O414-43 375 E4\"           物理          课内学习   2015/8/31  \n",
      "242250                None           小说          课外阅读   2015/8/31  \n",
      "242251   TP391.413 4112-2\"          计算机          课内学习   2015/8/31  \n",
      "242252           G122 204\"           小说          课外阅读   2015/8/31  \n",
      "242253                None          计算机          课内学习   2015/8/31  \n",
      "242254         B81-49 335\"           小说          课外阅读   2015/8/31  \n",
      "242255   H315-44 348.2 E2\"           出国          课内学习   2015/8/31  \n",
      "242256     B812.23-49 786\"           小说          课外阅读   2015/8/31  \n",
      "242257     B812.2-49 S773\"           哲学          课内学习   2015/8/31  \n",
      "242258         H313.1 849\"           考研          课内学习   2015/8/31  \n",
      "242259                None           小说          课外阅读   2015/8/31  \n",
      "242260     I561.84 R796-5\"           小说          课外阅读   2015/8/31  \n",
      "242261        B84-065 915\"           小说          课外阅读   2015/8/31  \n",
      "242262                None          计算机          课内学习   2015/8/31  \n",
      "242263   H313.1-43 508 E2\"           考研          课内学习   2015/8/31  \n",
      "242264  H319.4 343.2-2 E2\"           考研          课内学习   2015/8/31  \n",
      "242265   H319.4-42 1101.2\"           考研          课内学习   2015/8/31  \n",
      "242266        F830.91 385\"           小说          课外阅读   2015/8/31  \n",
      "242267    TN915.04-43 410\"          计算机          课内学习   2015/8/31  \n",
      "242268    I561.45 M194-16\"           小说          课外阅读   2015/8/31  \n",
      "242269      TN911.6-43 96\"          计算机          课内学习   2015/8/31  \n",
      "242270      TN911.6-43 96\"          计算机          课内学习   2015/8/31  \n",
      "242271        B842.1 S768\"           哲学          课内学习   2015/8/31  \n",
      "242272         B84-43 486\"           哲学          课内学习   2015/8/31  \n",
      "\n",
      "[482220 rows x 7 columns]\n"
     ]
    }
   ],
   "source": [
    "borrow_train_test[\"bookClassify\"] = borrow_train_test[\"bookName\"].apply(borrow_find)\n",
    "borrow_train_test[\"bookClassify2\"] = borrow_train_test[\"bookClassify\"].apply(borrow_find2)\n",
    "borrow_train_test[\"bookTimes\"] = borrow_train_test[\"borrowTime\"].apply(lambda x:x.strip('\"'))\n",
    "print borrow_train_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "borrow_bookClassify_count = borrow_train_test.groupby([\"id\",\"bookClassify\"])[\"bookTimes\"].count().unstack([\"bookClassify\"])\n",
    "borrow_bookClassify_count = borrow_bookClassify_count.rename(columns=dict(zip(borrow_bookClassify_count,\n",
    "                                                                              [str(i)+\"_count\" for i in borrow_bookClassify_count]))).reset_index().fillna(0)\n",
    "\n",
    "borrow_bookClassify2_count = borrow_train_test.groupby([\"id\",\"bookClassify2\"])[\"bookTimes\"].count().unstack([\"bookClassify2\"])\n",
    "borrow_bookClassify2_count = borrow_bookClassify2_count.rename(columns=dict(zip(borrow_bookClassify2_count,[str(i)+\"_count\" for i \n",
    "                                                                                                         in borrow_bookClassify2_count]))).reset_index().fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "borrow_bookTimes_count = borrow_train_test.groupby([\"id\",\"bookTimes\"])[\"bookClassify\"].count().unstack([\"bookTimes\"])\n",
    "borrow_bookTimes_count = borrow_bookTimes_count.rename(columns=dict(zip(borrow_bookTimes_count,[str(i)+\"_count\" for i \n",
    "                                                                                                         in borrow_bookTimes_count]))).reset_index().fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            id libraryNo          libraryTime\n",
      "0         3684         5  2013/09/01 08:42:50\n",
      "1        14314         6  2013/09/01 08:42:59\n",
      "2         5108         9  2013/09/01 08:43:32\n",
      "3         5826         7  2013/09/01 08:44:06\n",
      "4         7730         5  2013/09/01 08:44:25\n",
      "5         6668         7  2013/09/01 08:47:24\n",
      "6        11636         8  2013/09/01 08:48:24\n",
      "7         4036         6  2013/09/01 08:49:53\n",
      "8         4050         5  2013/09/01 08:50:01\n",
      "9         7434         5  2013/09/01 08:50:08\n",
      "10        7968         7  2013/09/01 08:52:00\n",
      "11        6956         6  2013/09/01 08:54:13\n",
      "12       20132         7  2013/09/01 08:57:33\n",
      "13        3166         5  2013/09/01 09:00:40\n",
      "14         940         5  2013/09/01 09:01:27\n",
      "15        7548         7  2013/09/01 09:01:30\n",
      "16        7262         7  2013/09/01 09:02:21\n",
      "17        1832         5  2013/09/01 09:10:47\n",
      "18        9660         7  2013/09/01 09:10:48\n",
      "19         476         6  2013/09/01 09:12:04\n",
      "20        7746         6  2013/09/01 09:13:30\n",
      "21        7976         6  2013/09/01 09:14:57\n",
      "22        6098         7  2013/09/01 09:15:01\n",
      "23        6196         6  2013/09/01 09:15:07\n",
      "24        4020         7  2013/09/01 09:15:45\n",
      "25         650         5  2013/09/01 09:20:06\n",
      "26        9548         6  2013/09/01 09:21:59\n",
      "27       10200         8  2013/09/01 09:22:04\n",
      "28        8128         7  2013/09/01 09:22:44\n",
      "29       20174         7  2013/09/01 09:24:00\n",
      "...        ...       ...                  ...\n",
      "1055166  28597        小门  2015/08/05 09:00:11\n",
      "1055167  29538        小门  2015/08/05 09:00:26\n",
      "1055168  11523       进门2  2015/08/05 09:01:14\n",
      "1055169  12816       进门4  2015/08/05 09:01:25\n",
      "1055170  29527       进门4  2015/08/05 09:01:29\n",
      "1055171   4129       进门5  2015/08/05 09:01:51\n",
      "1055172   4993       进门3  2015/08/05 09:02:04\n",
      "1055173     85        小门  2015/08/05 09:02:14\n",
      "1055174  27681        小门  2015/08/05 09:02:16\n",
      "1055175   9822       进门5  2015/08/05 09:02:17\n",
      "1055176  28605        小门  2015/08/05 09:02:20\n",
      "1055177  21476       进门5  2015/08/05 09:02:22\n",
      "1055178  29297       进门4  2015/08/05 09:02:31\n",
      "1055179  27475       进门3  2015/08/05 09:02:32\n",
      "1055180  13362       进门5  2015/08/05 09:02:39\n",
      "1055181   8153       进门5  2015/08/05 09:02:43\n",
      "1055182  18018       进门4  2015/08/05 09:02:45\n",
      "1055183  19067        小门  2015/08/05 09:03:01\n",
      "1055184  19067        小门  2015/08/05 09:03:04\n",
      "1055185  22314       进门3  2015/08/05 09:03:13\n",
      "1055186  15369       进门4  2015/08/05 09:03:28\n",
      "1055187  15048       进门3  2015/08/05 09:03:36\n",
      "1055188  10752       进门3  2015/08/05 09:03:49\n",
      "1055189  24857        小门  2015/08/05 09:04:09\n",
      "1055190   9711       进门4  2015/08/05 09:04:16\n",
      "1055191  29946       进门4  2015/08/05 09:04:30\n",
      "1055192   7622        小门  2015/08/05 09:04:33\n",
      "1055193   6354       进门3  2015/08/05 09:04:40\n",
      "1055194  26551       进门3  2015/08/05 09:05:08\n",
      "1055195   9668       进门5  2015/08/05 09:05:17\n",
      "\n",
      "[2067943 rows x 3 columns]\n"
     ]
    }
   ],
   "source": [
    "library_train = pd.read_table('data/train/library_train.txt',sep=',',header=None)\n",
    "library_train.columns = ['id','libraryNo','libraryTime']\n",
    "library_test = pd.read_table('data/test/library_final_test.txt',sep=',',header=-1)\n",
    "library_test.columns = ['id','libraryNo','libraryTime']\n",
    "library_train_test = pd.concat([library_train,library_test])\n",
    "print library_train_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "library = pd.DataFrame(library_train_test.groupby(['id'])[\"libraryTime\"].count()).reset_index().rename(columns={\"libraryTime\":\"libraryActionNum\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "library_train_test['libraryTime'] = library_train_test['libraryTime'].apply(lambda x: datetime.datetime.strptime(\n",
    "        x, \"%Y/%m/%d %H:%M:%S\").date())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "library_libraryTime_count = library_train_test.groupby([\"id\",\"libraryTime\"])[\"libraryNo\"].count().unstack([\"libraryTime\"])\n",
    "library_libraryTime_count = library_libraryTime_count.rename(columns=dict(zip(library_libraryTime_count,[str(i)+\"_library_count\" for i \n",
    "                                                                                                         in library_libraryTime_count]))).reset_index().fillna(0)\n",
    "\n",
    "library_libraryNo_count = library_train_test.groupby([\"id\",\"libraryNo\"])[\"libraryTime\"].count().unstack([\"libraryNo\"])\n",
    "library_libraryNo_count = library_libraryNo_count.rename(columns=dict(zip(library_libraryNo_count,[str(i)+\"_library_count\" for i \n",
    "                                                                                                         in library_libraryNo_count]))).reset_index().fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "train_test = pd.merge(train_test,card,how='left',on='id')\n",
    "train_test = pd.merge(train_test,card_how_amount,how='left',on='id')\n",
    "train_test = pd.merge(train_test,card_consume_amount.loc[:,[\"id\",\"换卡_count\",\"POS消费_mean\",\"卡充值_mean\",\"POS消费_sum\"\n",
    "                                                           ,\"POS消费_count\",\"圈存转账_sum\",\"POS消费_max\",\"POS消费_min\"\n",
    "                                                           ,\"圈存转账_mean\",\"卡充值_sum\" ,\"圈存转账_count\",\"卡充值_count\"\n",
    "                                                           ,\"卡补办_sum\"]],how='left',on='id')\n",
    "train_test = pd.merge(train_test,card_where_amount.loc[:,[\"id\",\"地点21_mean\",\"地点213_count\",\"地点72_sum\",\"地点283_sum\"\n",
    "                                                          ,\"地点232_mean\"]],how='left',on='id')\n",
    "train_test = pd.merge(train_test,card_time_amount.loc[:,[\"id\",\"2015-05-21_mean\",\"2014-09-05_mean\",\"2014-09-01_mean\",\"2014-10-27_mean\"\n",
    "                                                          ,\"2015-07-19_median\",\"2013-09-02_sum\",\"2014-12-10_min\",\"2014-09-26_mean\"\n",
    "                                                          ,\"2015-03-11_sum\",\"2015-02-05_min\",\"2015-07-23_count\"]],how='left',on='id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "train_test = pd.merge(train_test,borrow,how='left',on='id')\n",
    "train_test = pd.merge(train_test,borrow_bookClassify_count,how='left',on='id')\n",
    "train_test = pd.merge(train_test,borrow_bookClassify2_count,how='left',on='id')\n",
    "\n",
    "train_test = pd.merge(train_test,borrow_bookTimes_count.loc[:,[\"id\",\"2015/4/15_count\",\"2014/9/1_count\",\n",
    "                                                         \"2013/9/4_count\",\"2014/9/9_count\",\"2013/9/7_count\"\n",
    "                                                               ,\"2014/9/2_count\",\"2014/9/10_count\",\n",
    "                                                         \"2014/10/27_count\",\"2014/9/3_count\",\"2014/9/5_count\"\n",
    "                                                               ,\"2013/12/4_count\",\"2015/6/23_count\",\n",
    "                                                         \"2014/12/25_count\",\"2013/9/3_count\"]],how='left',on='id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_test = pd.merge(train_test,library,how='left',on='id')\n",
    "train_test = pd.merge(train_test,library_libraryTime_count.loc[:,[\"id\",\"2014-09-09_library_count\",\"2013-10-07_library_count\",\n",
    "                                                         \"2013-09-04_library_count\",\"2015-08-04_library_count\",\"2014-09-03_library_count\"\n",
    "                                                               ,\"2013-09-03_library_count\"]],how='left',on='id')\n",
    "train_test = pd.merge(train_test,library_libraryNo_count.loc[:,[\"id\",\"小门_library_count\",\"出门4_library_count\",\n",
    "                                                         \"出门3_library_count\"]],how='left',on='id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 把这种删除特征的方法下次用到random0.03045上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dropList = [\"公务员考试_count\",\"college_18\",\"其他_median\",\"校医院_median\",\"日语_count\",\"college_14\",\n",
    "           \"物理_count\",\"college_2\",\"college_1\",\"college_10\",\"校医院_count\",\"其他_min\",\"其他_count\",\n",
    "           \"法语_count\",\"三人研修室_count\"]\n",
    "train_test.drop(dropList,axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dropList2 = [\"2013/12/4_count\",\"2015/6/23_count\",\"2013/9/7_count\",\"单人研修室_count\",\"校医院_max\",\"2014/9/2_count\",\n",
    "           \"其他_max\"]\n",
    "train_test.drop(dropList2,axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dropList3 = [\"课外阅读_count\",\"校医院_min\",\"英语_count\",\"校医院_mean\",\"考研_count\",\"2014/9/5_count\",\n",
    "           \"2014/9/3_count\",\"college_15\",\"2014/9/9_count\",\"2014/12/25_count\",\"2013/9/3_count\"]\n",
    "train_test.drop(dropList3,axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train = train_test.loc[train_test.money.notnull(),:]\n",
    "test = train_test.loc[train_test.money.isnull(),:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10885, 133) (10961, 133)\n",
      "Index([u'id', u'money', u'score', u'college_num', u'college_orderate',\n",
      "       u'college_9', u'college_19', u'college_6', u'college_3', u'college_4',\n",
      "       ...\n",
      "       u'libraryActionNum', u'2014-09-09_library_count',\n",
      "       u'2013-10-07_library_count', u'2013-09-04_library_count',\n",
      "       u'2015-08-04_library_count', u'2014-09-03_library_count',\n",
      "       u'2013-09-03_library_count', u'小门_library_count', u'出门4_library_count',\n",
      "       u'出门3_library_count'],\n",
      "      dtype='object', length=133) Index([u'id', u'money', u'score', u'college_num', u'college_orderate',\n",
      "       u'college_9', u'college_19', u'college_6', u'college_3', u'college_4',\n",
      "       ...\n",
      "       u'libraryActionNum', u'2014-09-09_library_count',\n",
      "       u'2013-10-07_library_count', u'2013-09-04_library_count',\n",
      "       u'2015-08-04_library_count', u'2014-09-03_library_count',\n",
      "       u'2013-09-03_library_count', u'小门_library_count', u'出门4_library_count',\n",
      "       u'出门3_library_count'],\n",
      "      dtype='object', length=133)\n"
     ]
    }
   ],
   "source": [
    "print train.shape,test.shape\n",
    "print train.columns,test.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       money   score  college_num  college_orderate  college_9  college_19  \\\n",
      "id                                                                           \n",
      "0          0     1.0       2933.0          0.000341        1.0         0.0   \n",
      "1          0     2.0       2933.0          0.000682        2.0         0.0   \n",
      "8          0  1565.0       1572.0          0.995547        0.0         0.0   \n",
      "9          0  1570.0       1572.0          0.998728        0.0         0.0   \n",
      "10         0     1.0       2301.0          0.000435        0.0         0.0   \n",
      "11         0     2.0       2301.0          0.000869        0.0         0.0   \n",
      "19         0     7.0        963.0          0.007269        0.0         0.0   \n",
      "20         0   356.0        963.0          0.369678        0.0         0.0   \n",
      "21         0   381.0        963.0          0.395639        0.0         0.0   \n",
      "22         1  1734.0       2830.0          0.612721        0.0         0.0   \n",
      "23         2  2154.0       2830.0          0.761131        0.0         0.0   \n",
      "28         1  2245.0       2830.0          0.793286        0.0         0.0   \n",
      "29         0  2247.0       2830.0          0.793993        0.0         0.0   \n",
      "36         0  2269.0       2830.0          0.801767        0.0         0.0   \n",
      "37         0  2272.0       2830.0          0.802827        0.0         0.0   \n",
      "56         0  2365.0       2830.0          0.835689        0.0         0.0   \n",
      "57         0  2376.0       2830.0          0.839576        0.0         0.0   \n",
      "62         0  2433.0       2830.0          0.859717        0.0         0.0   \n",
      "63         0  2442.0       2830.0          0.862898        0.0         0.0   \n",
      "64         2  2457.0       2830.0          0.868198        0.0         0.0   \n",
      "65         0  2468.0       2830.0          0.872085        0.0         0.0   \n",
      "66         0  2864.0       2933.0          0.976475     2864.0         0.0   \n",
      "67         0  2865.0       2933.0          0.976816     2865.0         0.0   \n",
      "70         0  2872.0       2933.0          0.979202     2872.0         0.0   \n",
      "71         0  2873.0       2933.0          0.979543     2873.0         0.0   \n",
      "74         0  2882.0       2933.0          0.982612     2882.0         0.0   \n",
      "75         0  2885.0       2933.0          0.983635     2885.0         0.0   \n",
      "76         0  2890.0       2933.0          0.985339     2890.0         0.0   \n",
      "77         0  2895.0       2933.0          0.987044     2895.0         0.0   \n",
      "82         0  2906.0       2933.0          0.990794     2906.0         0.0   \n",
      "...      ...     ...          ...               ...        ...         ...   \n",
      "18867      3   894.0       2306.0          0.387684        0.0       894.0   \n",
      "18907      3    26.0        631.0          0.041204        0.0         0.0   \n",
      "19045      3   154.0        631.0          0.244057        0.0         0.0   \n",
      "19107      3   214.0        631.0          0.339144        0.0         0.0   \n",
      "19123      3     6.0        615.0          0.009756        0.0         0.0   \n",
      "19127      3    10.0        615.0          0.016260        0.0         0.0   \n",
      "19237      3   106.0        615.0          0.172358        0.0         0.0   \n",
      "19283      3   148.0        615.0          0.240650        0.0         0.0   \n",
      "19487      3    90.0        582.0          0.154639        0.0         0.0   \n",
      "19565      3   168.0        582.0          0.288660        0.0         0.0   \n",
      "19765      3   121.0        963.0          0.125649        0.0         0.0   \n",
      "20073      3    58.0        749.0          0.077437        0.0         0.0   \n",
      "20201      3   186.0        749.0          0.248331        0.0         0.0   \n",
      "20237      3   222.0        749.0          0.296395        0.0         0.0   \n",
      "20265      3   250.0        749.0          0.333778        0.0         0.0   \n",
      "20269      3   254.0        749.0          0.339119        0.0         0.0   \n",
      "20627      3   202.0        369.0          0.547425        0.0         0.0   \n",
      "20635      3   210.0        369.0          0.569106        0.0         0.0   \n",
      "20715      3    64.0        193.0          0.331606        0.0         0.0   \n",
      "20767      3   114.0        193.0          0.590674        0.0         0.0   \n",
      "20981      3   189.0        510.0          0.370588        0.0         0.0   \n",
      "21097      3  1000.0       2716.0          0.368189        0.0         0.0   \n",
      "21247      3  1150.0       2716.0          0.423417        0.0         0.0   \n",
      "21249      3  1152.0       2716.0          0.424153        0.0         0.0   \n",
      "21511      3  1414.0       2716.0          0.520619        0.0         0.0   \n",
      "21567      3  1470.0       2716.0          0.541237        0.0         0.0   \n",
      "21653      3  1556.0       2716.0          0.572901        0.0         0.0   \n",
      "21757      3  1660.0       2716.0          0.611193        0.0         0.0   \n",
      "21853      3  1756.0       2716.0          0.646539        0.0         0.0   \n",
      "21959      3  1862.0       2716.0          0.685567        0.0         0.0   \n",
      "\n",
      "       college_6  college_3  college_4  college_7   ...    \\\n",
      "id                                                  ...     \n",
      "0            0.0        0.0        0.0        0.0   ...     \n",
      "1            0.0        0.0        0.0        0.0   ...     \n",
      "8         1565.0        0.0        0.0        0.0   ...     \n",
      "9         1570.0        0.0        0.0        0.0   ...     \n",
      "10           0.0        1.0        0.0        0.0   ...     \n",
      "11           0.0        2.0        0.0        0.0   ...     \n",
      "19           0.0        0.0        0.0        7.0   ...     \n",
      "20           0.0        0.0        0.0      356.0   ...     \n",
      "21           0.0        0.0        0.0      381.0   ...     \n",
      "22           0.0        0.0        0.0        0.0   ...     \n",
      "23           0.0        0.0        0.0        0.0   ...     \n",
      "28           0.0        0.0        0.0        0.0   ...     \n",
      "29           0.0        0.0        0.0        0.0   ...     \n",
      "36           0.0        0.0        0.0        0.0   ...     \n",
      "37           0.0        0.0        0.0        0.0   ...     \n",
      "56           0.0        0.0        0.0        0.0   ...     \n",
      "57           0.0        0.0        0.0        0.0   ...     \n",
      "62           0.0        0.0        0.0        0.0   ...     \n",
      "63           0.0        0.0        0.0        0.0   ...     \n",
      "64           0.0        0.0        0.0        0.0   ...     \n",
      "65           0.0        0.0        0.0        0.0   ...     \n",
      "66           0.0        0.0        0.0        0.0   ...     \n",
      "67           0.0        0.0        0.0        0.0   ...     \n",
      "70           0.0        0.0        0.0        0.0   ...     \n",
      "71           0.0        0.0        0.0        0.0   ...     \n",
      "74           0.0        0.0        0.0        0.0   ...     \n",
      "75           0.0        0.0        0.0        0.0   ...     \n",
      "76           0.0        0.0        0.0        0.0   ...     \n",
      "77           0.0        0.0        0.0        0.0   ...     \n",
      "82           0.0        0.0        0.0        0.0   ...     \n",
      "...          ...        ...        ...        ...   ...     \n",
      "18867        0.0        0.0        0.0        0.0   ...     \n",
      "18907        0.0        0.0        0.0        0.0   ...     \n",
      "19045        0.0        0.0        0.0        0.0   ...     \n",
      "19107        0.0        0.0        0.0        0.0   ...     \n",
      "19123        0.0        0.0        0.0        0.0   ...     \n",
      "19127        0.0        0.0        0.0        0.0   ...     \n",
      "19237        0.0        0.0        0.0        0.0   ...     \n",
      "19283        0.0        0.0        0.0        0.0   ...     \n",
      "19487        0.0        0.0        0.0        0.0   ...     \n",
      "19565        0.0        0.0        0.0        0.0   ...     \n",
      "19765        0.0        0.0        0.0      121.0   ...     \n",
      "20073        0.0        0.0        0.0        0.0   ...     \n",
      "20201        0.0        0.0        0.0        0.0   ...     \n",
      "20237        0.0        0.0        0.0        0.0   ...     \n",
      "20265        0.0        0.0        0.0        0.0   ...     \n",
      "20269        0.0        0.0        0.0        0.0   ...     \n",
      "20627        0.0        0.0        0.0        0.0   ...     \n",
      "20635        0.0        0.0        0.0        0.0   ...     \n",
      "20715        0.0        0.0        0.0        0.0   ...     \n",
      "20767        0.0        0.0        0.0        0.0   ...     \n",
      "20981        0.0        0.0        0.0        0.0   ...     \n",
      "21097        0.0        0.0        0.0        0.0   ...     \n",
      "21247        0.0        0.0        0.0        0.0   ...     \n",
      "21249        0.0        0.0        0.0        0.0   ...     \n",
      "21511        0.0        0.0        0.0        0.0   ...     \n",
      "21567        0.0        0.0        0.0        0.0   ...     \n",
      "21653        0.0        0.0        0.0        0.0   ...     \n",
      "21757        0.0        0.0        0.0        0.0   ...     \n",
      "21853        0.0        0.0        0.0        0.0   ...     \n",
      "21959        0.0        0.0        0.0        0.0   ...     \n",
      "\n",
      "       2014-09-09_library_count  2013-10-07_library_count  \\\n",
      "id                                                          \n",
      "0                           0.0                       0.0   \n",
      "1                           0.0                       0.0   \n",
      "8                          -1.0                      -1.0   \n",
      "9                          -1.0                      -1.0   \n",
      "10                          0.0                       0.0   \n",
      "11                         -1.0                      -1.0   \n",
      "19                          0.0                       0.0   \n",
      "20                          0.0                       1.0   \n",
      "21                          0.0                       0.0   \n",
      "22                          0.0                       0.0   \n",
      "23                          0.0                       0.0   \n",
      "28                          0.0                       0.0   \n",
      "29                          2.0                       0.0   \n",
      "36                          0.0                       0.0   \n",
      "37                          0.0                       0.0   \n",
      "56                          0.0                       0.0   \n",
      "57                          0.0                       0.0   \n",
      "62                          0.0                       0.0   \n",
      "63                         -1.0                      -1.0   \n",
      "64                          0.0                       0.0   \n",
      "65                          0.0                       0.0   \n",
      "66                          0.0                       0.0   \n",
      "67                          0.0                       0.0   \n",
      "70                          0.0                       0.0   \n",
      "71                          0.0                       0.0   \n",
      "74                          0.0                       0.0   \n",
      "75                          0.0                       0.0   \n",
      "76                          0.0                       0.0   \n",
      "77                          0.0                       0.0   \n",
      "82                          0.0                       0.0   \n",
      "...                         ...                       ...   \n",
      "18867                       0.0                       0.0   \n",
      "18907                       0.0                       0.0   \n",
      "19045                       2.0                       0.0   \n",
      "19107                       0.0                       0.0   \n",
      "19123                       0.0                       0.0   \n",
      "19127                       0.0                       0.0   \n",
      "19237                      -1.0                      -1.0   \n",
      "19283                       0.0                       0.0   \n",
      "19487                       0.0                       0.0   \n",
      "19565                       0.0                       0.0   \n",
      "19765                       0.0                       0.0   \n",
      "20073                       2.0                       0.0   \n",
      "20201                       0.0                       0.0   \n",
      "20237                       0.0                       0.0   \n",
      "20265                       0.0                       0.0   \n",
      "20269                       0.0                       0.0   \n",
      "20627                       0.0                       0.0   \n",
      "20635                       0.0                       0.0   \n",
      "20715                       2.0                       0.0   \n",
      "20767                       2.0                       0.0   \n",
      "20981                       0.0                       0.0   \n",
      "21097                      -1.0                      -1.0   \n",
      "21247                      -1.0                      -1.0   \n",
      "21249                       0.0                       0.0   \n",
      "21511                      -1.0                      -1.0   \n",
      "21567                      -1.0                      -1.0   \n",
      "21653                       0.0                       0.0   \n",
      "21757                      -1.0                      -1.0   \n",
      "21853                       0.0                       0.0   \n",
      "21959                      -1.0                      -1.0   \n",
      "\n",
      "       2013-09-04_library_count  2015-08-04_library_count  \\\n",
      "id                                                          \n",
      "0                           0.0                       0.0   \n",
      "1                           0.0                       0.0   \n",
      "8                          -1.0                      -1.0   \n",
      "9                          -1.0                      -1.0   \n",
      "10                          0.0                       0.0   \n",
      "11                         -1.0                      -1.0   \n",
      "19                          0.0                       0.0   \n",
      "20                          0.0                       0.0   \n",
      "21                          0.0                       0.0   \n",
      "22                          0.0                       0.0   \n",
      "23                          0.0                       0.0   \n",
      "28                          0.0                       0.0   \n",
      "29                          0.0                       0.0   \n",
      "36                          0.0                       0.0   \n",
      "37                          0.0                       0.0   \n",
      "56                          0.0                       0.0   \n",
      "57                          0.0                       0.0   \n",
      "62                          0.0                       0.0   \n",
      "63                         -1.0                      -1.0   \n",
      "64                          0.0                       0.0   \n",
      "65                          0.0                       0.0   \n",
      "66                          0.0                       0.0   \n",
      "67                          0.0                       0.0   \n",
      "70                          0.0                       0.0   \n",
      "71                          0.0                       0.0   \n",
      "74                          0.0                       0.0   \n",
      "75                          0.0                       0.0   \n",
      "76                          0.0                       0.0   \n",
      "77                          0.0                       0.0   \n",
      "82                          1.0                       0.0   \n",
      "...                         ...                       ...   \n",
      "18867                       0.0                       0.0   \n",
      "18907                       0.0                       0.0   \n",
      "19045                       0.0                       0.0   \n",
      "19107                       0.0                       0.0   \n",
      "19123                       0.0                       0.0   \n",
      "19127                       0.0                       0.0   \n",
      "19237                      -1.0                      -1.0   \n",
      "19283                       0.0                       0.0   \n",
      "19487                       0.0                       0.0   \n",
      "19565                       0.0                       0.0   \n",
      "19765                       0.0                       0.0   \n",
      "20073                       0.0                       0.0   \n",
      "20201                       0.0                       0.0   \n",
      "20237                       0.0                       0.0   \n",
      "20265                       0.0                       0.0   \n",
      "20269                       0.0                       0.0   \n",
      "20627                       0.0                       0.0   \n",
      "20635                       0.0                       0.0   \n",
      "20715                       0.0                       0.0   \n",
      "20767                       0.0                       0.0   \n",
      "20981                       0.0                       0.0   \n",
      "21097                      -1.0                      -1.0   \n",
      "21247                      -1.0                      -1.0   \n",
      "21249                       0.0                       0.0   \n",
      "21511                      -1.0                      -1.0   \n",
      "21567                      -1.0                      -1.0   \n",
      "21653                       0.0                       0.0   \n",
      "21757                      -1.0                      -1.0   \n",
      "21853                       0.0                       0.0   \n",
      "21959                      -1.0                      -1.0   \n",
      "\n",
      "       2014-09-03_library_count  2013-09-03_library_count  小门_library_count  \\\n",
      "id                                                                            \n",
      "0                           0.0                       0.0               0.0   \n",
      "1                           0.0                       0.0               0.0   \n",
      "8                          -1.0                      -1.0              -1.0   \n",
      "9                          -1.0                      -1.0              -1.0   \n",
      "10                          0.0                       0.0               0.0   \n",
      "11                         -1.0                      -1.0              -1.0   \n",
      "19                          0.0                       0.0               0.0   \n",
      "20                          0.0                       0.0               0.0   \n",
      "21                          0.0                       0.0               0.0   \n",
      "22                          0.0                       0.0               0.0   \n",
      "23                          0.0                       0.0               2.0   \n",
      "28                          0.0                       0.0               2.0   \n",
      "29                          5.0                       0.0               5.0   \n",
      "36                          0.0                       0.0               0.0   \n",
      "37                          0.0                       0.0               0.0   \n",
      "56                          0.0                       0.0               0.0   \n",
      "57                          0.0                       0.0               0.0   \n",
      "62                          0.0                       0.0               0.0   \n",
      "63                         -1.0                      -1.0              -1.0   \n",
      "64                          0.0                       0.0               0.0   \n",
      "65                          0.0                       0.0               0.0   \n",
      "66                          0.0                       0.0               0.0   \n",
      "67                          0.0                       0.0               0.0   \n",
      "70                          0.0                       1.0               0.0   \n",
      "71                          0.0                       0.0               0.0   \n",
      "74                          0.0                       0.0               1.0   \n",
      "75                          0.0                       0.0              18.0   \n",
      "76                          0.0                       0.0               0.0   \n",
      "77                          0.0                       0.0               0.0   \n",
      "82                          0.0                       0.0               0.0   \n",
      "...                         ...                       ...               ...   \n",
      "18867                       0.0                       0.0               3.0   \n",
      "18907                       2.0                       0.0              15.0   \n",
      "19045                       0.0                       0.0              40.0   \n",
      "19107                       0.0                       0.0               4.0   \n",
      "19123                       0.0                       0.0               0.0   \n",
      "19127                       0.0                       0.0               1.0   \n",
      "19237                      -1.0                      -1.0              -1.0   \n",
      "19283                       0.0                       0.0               0.0   \n",
      "19487                       0.0                       0.0               0.0   \n",
      "19565                       0.0                       0.0               0.0   \n",
      "19765                       0.0                       0.0               0.0   \n",
      "20073                       0.0                       0.0               4.0   \n",
      "20201                       0.0                       0.0               0.0   \n",
      "20237                       0.0                       0.0              19.0   \n",
      "20265                       0.0                       0.0               0.0   \n",
      "20269                       0.0                       0.0               1.0   \n",
      "20627                       0.0                       0.0               3.0   \n",
      "20635                       0.0                       0.0              19.0   \n",
      "20715                       0.0                       0.0              13.0   \n",
      "20767                       2.0                       0.0               1.0   \n",
      "20981                       0.0                       0.0               0.0   \n",
      "21097                      -1.0                      -1.0              -1.0   \n",
      "21247                      -1.0                      -1.0              -1.0   \n",
      "21249                       0.0                       0.0               0.0   \n",
      "21511                      -1.0                      -1.0              -1.0   \n",
      "21567                      -1.0                      -1.0              -1.0   \n",
      "21653                       0.0                       0.0               0.0   \n",
      "21757                      -1.0                      -1.0              -1.0   \n",
      "21853                       0.0                       0.0               0.0   \n",
      "21959                      -1.0                      -1.0              -1.0   \n",
      "\n",
      "       出门4_library_count  出门3_library_count  isnull  \n",
      "id                                                   \n",
      "0                    0.0                0.0      14  \n",
      "1                    0.0                1.0      14  \n",
      "8                   -1.0               -1.0      28  \n",
      "9                   -1.0               -1.0      28  \n",
      "10                   1.0                0.0      14  \n",
      "11                  -1.0               -1.0      10  \n",
      "19                   2.0                0.0      14  \n",
      "20                   0.0                1.0       0  \n",
      "21                   0.0                0.0      14  \n",
      "22                   2.0                2.0       0  \n",
      "23                   0.0                0.0       4  \n",
      "28                   4.0                5.0       0  \n",
      "29                  46.0               10.0       0  \n",
      "36                   0.0                0.0      14  \n",
      "37                   2.0                6.0      14  \n",
      "56                   0.0                1.0       4  \n",
      "57                   0.0                0.0      18  \n",
      "62                   0.0                0.0       0  \n",
      "63                  -1.0               -1.0     115  \n",
      "64                   4.0                3.0       0  \n",
      "65                   0.0                0.0      14  \n",
      "66                   1.0                3.0       0  \n",
      "67                   0.0                0.0       4  \n",
      "70                   5.0                7.0      18  \n",
      "71                   8.0                4.0      14  \n",
      "74                   0.0                4.0      14  \n",
      "75                  14.0                2.0      14  \n",
      "76                   0.0                0.0      14  \n",
      "77                   1.0                2.0       0  \n",
      "82                   1.0                7.0       0  \n",
      "...                  ...                ...     ...  \n",
      "18867                1.0                2.0       0  \n",
      "18907                2.0                4.0       0  \n",
      "19045               19.0               16.0       0  \n",
      "19107                0.0                0.0       0  \n",
      "19123                5.0                3.0       4  \n",
      "19127                1.0                1.0       4  \n",
      "19237               -1.0               -1.0      24  \n",
      "19283                0.0                6.0       0  \n",
      "19487                0.0                0.0      14  \n",
      "19565                4.0                3.0       0  \n",
      "19765               20.0                6.0       0  \n",
      "20073               11.0                9.0       0  \n",
      "20201                3.0               11.0       0  \n",
      "20237               16.0                0.0       4  \n",
      "20265                3.0                0.0       0  \n",
      "20269                6.0               22.0       4  \n",
      "20627                8.0                6.0       0  \n",
      "20635                3.0                6.0       4  \n",
      "20715                2.0                2.0       0  \n",
      "20767               59.0               33.0       0  \n",
      "20981                9.0                3.0       0  \n",
      "21097               -1.0               -1.0      28  \n",
      "21247               -1.0               -1.0      24  \n",
      "21249                0.0                0.0      18  \n",
      "21511               -1.0               -1.0      28  \n",
      "21567               -1.0               -1.0      10  \n",
      "21653                0.0                0.0       0  \n",
      "21757               -1.0               -1.0      14  \n",
      "21853                0.0                0.0       0  \n",
      "21959               -1.0               -1.0      24  \n",
      "\n",
      "[26176 rows x 133 columns]\n",
      "        score  college_num  college_orderate  college_9  college_19  \\\n",
      "id                                                                    \n",
      "6      1575.0       2306.0          0.683001        0.0      1575.0   \n",
      "7      1372.0       2306.0          0.594970        0.0      1372.0   \n",
      "12     1351.0       2418.0          0.558726        0.0         0.0   \n",
      "13       -1.0         -1.0         -1.000000       -1.0        -1.0   \n",
      "14       -1.0         -1.0         -1.000000       -1.0        -1.0   \n",
      "24     1623.0       2830.0          0.573498        0.0         0.0   \n",
      "25       -1.0         -1.0         -1.000000       -1.0        -1.0   \n",
      "26       -1.0         -1.0         -1.000000       -1.0        -1.0   \n",
      "27      607.0       2830.0          0.214488        0.0         0.0   \n",
      "30     1528.0       2830.0          0.539929        0.0         0.0   \n",
      "31      285.0        582.0          0.489691        0.0         0.0   \n",
      "40      183.0       2782.0          0.065780        0.0         0.0   \n",
      "41       68.0       2306.0          0.029488        0.0        68.0   \n",
      "42      497.0       2716.0          0.182990        0.0         0.0   \n",
      "43       -1.0         -1.0         -1.000000       -1.0        -1.0   \n",
      "44      446.0       1572.0          0.283715        0.0         0.0   \n",
      "45     2018.0       2830.0          0.713074        0.0         0.0   \n",
      "50     2193.0       2933.0          0.747699     2193.0         0.0   \n",
      "51      768.0       2301.0          0.333768        0.0         0.0   \n",
      "52      462.0        510.0          0.905882        0.0         0.0   \n",
      "53     1247.0       2053.0          0.607404        0.0         0.0   \n",
      "54     2218.0       2933.0          0.756222     2218.0         0.0   \n",
      "55       82.0       2418.0          0.033912        0.0         0.0   \n",
      "58     2522.0       2830.0          0.891166        0.0         0.0   \n",
      "59      482.0        582.0          0.828179        0.0         0.0   \n",
      "68     2072.0       2782.0          0.744788        0.0         0.0   \n",
      "69       -1.0         -1.0         -1.000000       -1.0        -1.0   \n",
      "78     1757.0       2306.0          0.761925        0.0      1757.0   \n",
      "79      386.0       2053.0          0.188018        0.0         0.0   \n",
      "80     1045.0       2716.0          0.384757        0.0         0.0   \n",
      "...       ...          ...               ...        ...         ...   \n",
      "32591    -1.0         -1.0         -1.000000       -1.0        -1.0   \n",
      "32592    -1.0         -1.0         -1.000000       -1.0        -1.0   \n",
      "32593   917.0       2830.0          0.324028        0.0         0.0   \n",
      "32594  1272.0       2933.0          0.433686     1272.0         0.0   \n",
      "32595  2747.0       2830.0          0.970671        0.0         0.0   \n",
      "32596  2108.0       2933.0          0.718718     2108.0         0.0   \n",
      "32597   140.0       2933.0          0.047733      140.0         0.0   \n",
      "32598   219.0        582.0          0.376289        0.0         0.0   \n",
      "32599  1470.0       2306.0          0.637467        0.0      1470.0   \n",
      "32600   379.0        963.0          0.393562        0.0         0.0   \n",
      "32601   118.0       2418.0          0.048801        0.0         0.0   \n",
      "32604  1383.0       2933.0          0.471531     1383.0         0.0   \n",
      "32605   215.0       2053.0          0.104725        0.0         0.0   \n",
      "32616    76.0        749.0          0.101469        0.0         0.0   \n",
      "32617    85.0       2053.0          0.041403        0.0         0.0   \n",
      "32620  1867.0       2933.0          0.636550     1867.0         0.0   \n",
      "32621  1736.0       2782.0          0.624012        0.0         0.0   \n",
      "32628  1677.0       2782.0          0.602804        0.0         0.0   \n",
      "32629  1047.0       2782.0          0.376348        0.0         0.0   \n",
      "32632  1298.0       1572.0          0.825700        0.0         0.0   \n",
      "32633    84.0       2306.0          0.036427        0.0        84.0   \n",
      "32634  1232.0       2933.0          0.420048     1232.0         0.0   \n",
      "32635    -1.0         -1.0         -1.000000       -1.0        -1.0   \n",
      "32636  2100.0       2301.0          0.912647        0.0         0.0   \n",
      "32637  1055.0       2830.0          0.372792        0.0         0.0   \n",
      "32651  2642.0       2782.0          0.949676        0.0         0.0   \n",
      "32660   749.0        749.0          1.000000        0.0         0.0   \n",
      "32661   911.0       2782.0          0.327462        0.0         0.0   \n",
      "32662   880.0       2418.0          0.363937        0.0         0.0   \n",
      "32663    -1.0         -1.0         -1.000000       -1.0        -1.0   \n",
      "\n",
      "       college_6  college_3  college_4  college_7  college_8   ...    \\\n",
      "id                                                             ...     \n",
      "6            0.0        0.0        0.0        0.0        0.0   ...     \n",
      "7            0.0        0.0        0.0        0.0        0.0   ...     \n",
      "12           0.0        0.0     1351.0        0.0        0.0   ...     \n",
      "13          -1.0       -1.0       -1.0       -1.0       -1.0   ...     \n",
      "14          -1.0       -1.0       -1.0       -1.0       -1.0   ...     \n",
      "24           0.0        0.0        0.0        0.0     1623.0   ...     \n",
      "25          -1.0       -1.0       -1.0       -1.0       -1.0   ...     \n",
      "26          -1.0       -1.0       -1.0       -1.0       -1.0   ...     \n",
      "27           0.0        0.0        0.0        0.0      607.0   ...     \n",
      "30           0.0        0.0        0.0        0.0     1528.0   ...     \n",
      "31           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "40           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "41           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "42           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "43          -1.0       -1.0       -1.0       -1.0       -1.0   ...     \n",
      "44         446.0        0.0        0.0        0.0        0.0   ...     \n",
      "45           0.0        0.0        0.0        0.0     2018.0   ...     \n",
      "50           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "51           0.0      768.0        0.0        0.0        0.0   ...     \n",
      "52           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "53           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "54           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "55           0.0        0.0       82.0        0.0        0.0   ...     \n",
      "58           0.0        0.0        0.0        0.0     2522.0   ...     \n",
      "59           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "68           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "69          -1.0       -1.0       -1.0       -1.0       -1.0   ...     \n",
      "78           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "79           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "80           0.0        0.0        0.0        0.0        0.0   ...     \n",
      "...          ...        ...        ...        ...        ...   ...     \n",
      "32591       -1.0       -1.0       -1.0       -1.0       -1.0   ...     \n",
      "32592       -1.0       -1.0       -1.0       -1.0       -1.0   ...     \n",
      "32593        0.0        0.0        0.0        0.0      917.0   ...     \n",
      "32594        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32595        0.0        0.0        0.0        0.0     2747.0   ...     \n",
      "32596        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32597        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32598        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32599        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32600        0.0        0.0        0.0      379.0        0.0   ...     \n",
      "32601        0.0        0.0      118.0        0.0        0.0   ...     \n",
      "32604        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32605        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32616        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32617        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32620        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32621        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32628        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32629        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32632     1298.0        0.0        0.0        0.0        0.0   ...     \n",
      "32633        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32634        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32635       -1.0       -1.0       -1.0       -1.0       -1.0   ...     \n",
      "32636        0.0     2100.0        0.0        0.0        0.0   ...     \n",
      "32637        0.0        0.0        0.0        0.0     1055.0   ...     \n",
      "32651        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32660        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32661        0.0        0.0        0.0        0.0        0.0   ...     \n",
      "32662        0.0        0.0      880.0        0.0        0.0   ...     \n",
      "32663       -1.0       -1.0       -1.0       -1.0       -1.0   ...     \n",
      "\n",
      "       2014-09-09_library_count  2013-10-07_library_count  \\\n",
      "id                                                          \n",
      "6                           0.0                       0.0   \n",
      "7                           0.0                       0.0   \n",
      "12                          0.0                       0.0   \n",
      "13                         -1.0                      -1.0   \n",
      "14                         -1.0                      -1.0   \n",
      "24                         -1.0                      -1.0   \n",
      "25                         -1.0                      -1.0   \n",
      "26                         -1.0                      -1.0   \n",
      "27                          0.0                       0.0   \n",
      "30                          0.0                       0.0   \n",
      "31                          0.0                       0.0   \n",
      "40                          2.0                       0.0   \n",
      "41                          0.0                       0.0   \n",
      "42                         -1.0                      -1.0   \n",
      "43                         -1.0                      -1.0   \n",
      "44                          2.0                       0.0   \n",
      "45                          0.0                       0.0   \n",
      "50                          0.0                       0.0   \n",
      "51                          0.0                       0.0   \n",
      "52                          0.0                       0.0   \n",
      "53                          0.0                       0.0   \n",
      "54                          0.0                       0.0   \n",
      "55                          0.0                       0.0   \n",
      "58                          0.0                       0.0   \n",
      "59                          0.0                       0.0   \n",
      "68                          0.0                       3.0   \n",
      "69                         -1.0                      -1.0   \n",
      "78                          0.0                       0.0   \n",
      "79                          0.0                       0.0   \n",
      "80                         -1.0                      -1.0   \n",
      "...                         ...                       ...   \n",
      "32591                      -1.0                      -1.0   \n",
      "32592                      -1.0                      -1.0   \n",
      "32593                       0.0                       0.0   \n",
      "32594                       0.0                       0.0   \n",
      "32595                       0.0                       2.0   \n",
      "32596                       0.0                       0.0   \n",
      "32597                       0.0                       0.0   \n",
      "32598                       0.0                       0.0   \n",
      "32599                       0.0                       0.0   \n",
      "32600                       0.0                       0.0   \n",
      "32601                       0.0                       0.0   \n",
      "32604                       0.0                       0.0   \n",
      "32605                       0.0                       0.0   \n",
      "32616                       0.0                       0.0   \n",
      "32617                       0.0                       0.0   \n",
      "32620                       0.0                       0.0   \n",
      "32621                       0.0                       0.0   \n",
      "32628                       0.0                       0.0   \n",
      "32629                       0.0                       0.0   \n",
      "32632                      -1.0                      -1.0   \n",
      "32633                       0.0                       0.0   \n",
      "32634                       0.0                       0.0   \n",
      "32635                      -1.0                      -1.0   \n",
      "32636                      -1.0                      -1.0   \n",
      "32637                      -1.0                      -1.0   \n",
      "32651                       0.0                       0.0   \n",
      "32660                       0.0                       0.0   \n",
      "32661                       0.0                       0.0   \n",
      "32662                       0.0                       0.0   \n",
      "32663                      -1.0                      -1.0   \n",
      "\n",
      "       2013-09-04_library_count  2015-08-04_library_count  \\\n",
      "id                                                          \n",
      "6                           0.0                       0.0   \n",
      "7                           0.0                       0.0   \n",
      "12                          0.0                       0.0   \n",
      "13                         -1.0                      -1.0   \n",
      "14                         -1.0                      -1.0   \n",
      "24                         -1.0                      -1.0   \n",
      "25                         -1.0                      -1.0   \n",
      "26                         -1.0                      -1.0   \n",
      "27                          0.0                       0.0   \n",
      "30                          0.0                       0.0   \n",
      "31                          0.0                       0.0   \n",
      "40                          0.0                       0.0   \n",
      "41                          0.0                       0.0   \n",
      "42                         -1.0                      -1.0   \n",
      "43                         -1.0                      -1.0   \n",
      "44                          0.0                       0.0   \n",
      "45                          0.0                       0.0   \n",
      "50                          0.0                       0.0   \n",
      "51                          0.0                       0.0   \n",
      "52                          0.0                       0.0   \n",
      "53                          0.0                       0.0   \n",
      "54                          0.0                       0.0   \n",
      "55                          0.0                       0.0   \n",
      "58                          0.0                       0.0   \n",
      "59                          0.0                       0.0   \n",
      "68                          1.0                       0.0   \n",
      "69                         -1.0                      -1.0   \n",
      "78                          0.0                       0.0   \n",
      "79                          0.0                       0.0   \n",
      "80                         -1.0                      -1.0   \n",
      "...                         ...                       ...   \n",
      "32591                      -1.0                      -1.0   \n",
      "32592                      -1.0                      -1.0   \n",
      "32593                       0.0                       0.0   \n",
      "32594                       0.0                       0.0   \n",
      "32595                       0.0                       0.0   \n",
      "32596                       0.0                       0.0   \n",
      "32597                       0.0                       0.0   \n",
      "32598                       0.0                       0.0   \n",
      "32599                       0.0                       0.0   \n",
      "32600                       0.0                       0.0   \n",
      "32601                       0.0                       0.0   \n",
      "32604                       0.0                       0.0   \n",
      "32605                       0.0                       0.0   \n",
      "32616                       0.0                       0.0   \n",
      "32617                       0.0                       0.0   \n",
      "32620                       0.0                       2.0   \n",
      "32621                       0.0                       0.0   \n",
      "32628                       0.0                       0.0   \n",
      "32629                       0.0                       0.0   \n",
      "32632                      -1.0                      -1.0   \n",
      "32633                       0.0                       0.0   \n",
      "32634                       0.0                       0.0   \n",
      "32635                      -1.0                      -1.0   \n",
      "32636                      -1.0                      -1.0   \n",
      "32637                      -1.0                      -1.0   \n",
      "32651                       0.0                       0.0   \n",
      "32660                       0.0                       0.0   \n",
      "32661                       0.0                       0.0   \n",
      "32662                       0.0                       0.0   \n",
      "32663                      -1.0                      -1.0   \n",
      "\n",
      "       2014-09-03_library_count  2013-09-03_library_count  小门_library_count  \\\n",
      "id                                                                            \n",
      "6                           0.0                       0.0               0.0   \n",
      "7                           1.0                       0.0               0.0   \n",
      "12                          0.0                       0.0               0.0   \n",
      "13                         -1.0                      -1.0              -1.0   \n",
      "14                         -1.0                      -1.0              -1.0   \n",
      "24                         -1.0                      -1.0              -1.0   \n",
      "25                         -1.0                      -1.0              -1.0   \n",
      "26                         -1.0                      -1.0              -1.0   \n",
      "27                          0.0                       0.0               0.0   \n",
      "30                          0.0                       0.0               1.0   \n",
      "31                          0.0                       0.0               1.0   \n",
      "40                          2.0                       0.0               0.0   \n",
      "41                          0.0                       0.0               0.0   \n",
      "42                         -1.0                      -1.0              -1.0   \n",
      "43                         -1.0                      -1.0              -1.0   \n",
      "44                          0.0                       0.0              13.0   \n",
      "45                          0.0                       0.0               0.0   \n",
      "50                          0.0                       0.0               0.0   \n",
      "51                          0.0                       0.0              32.0   \n",
      "52                          0.0                       0.0               0.0   \n",
      "53                          0.0                       0.0               0.0   \n",
      "54                          0.0                       0.0               9.0   \n",
      "55                          0.0                       0.0               0.0   \n",
      "58                          0.0                       0.0               1.0   \n",
      "59                          1.0                       0.0               0.0   \n",
      "68                          0.0                       2.0               1.0   \n",
      "69                         -1.0                      -1.0              -1.0   \n",
      "78                          0.0                       0.0               1.0   \n",
      "79                          0.0                       0.0              88.0   \n",
      "80                         -1.0                      -1.0              -1.0   \n",
      "...                         ...                       ...               ...   \n",
      "32591                      -1.0                      -1.0              -1.0   \n",
      "32592                      -1.0                      -1.0              -1.0   \n",
      "32593                       0.0                       0.0               0.0   \n",
      "32594                       0.0                       0.0              14.0   \n",
      "32595                       0.0                       0.0               2.0   \n",
      "32596                       0.0                       0.0              10.0   \n",
      "32597                       0.0                       0.0               0.0   \n",
      "32598                       0.0                       0.0               2.0   \n",
      "32599                       0.0                       0.0               1.0   \n",
      "32600                       0.0                       0.0               0.0   \n",
      "32601                       0.0                       0.0              32.0   \n",
      "32604                       0.0                       0.0               8.0   \n",
      "32605                       0.0                       0.0               7.0   \n",
      "32616                       0.0                       0.0               0.0   \n",
      "32617                       0.0                       0.0               0.0   \n",
      "32620                       0.0                       0.0               4.0   \n",
      "32621                       0.0                       0.0               0.0   \n",
      "32628                       0.0                       0.0               0.0   \n",
      "32629                       0.0                       0.0               0.0   \n",
      "32632                      -1.0                      -1.0              -1.0   \n",
      "32633                       0.0                       0.0               0.0   \n",
      "32634                       0.0                       0.0               0.0   \n",
      "32635                      -1.0                      -1.0              -1.0   \n",
      "32636                      -1.0                      -1.0              -1.0   \n",
      "32637                      -1.0                      -1.0              -1.0   \n",
      "32651                       0.0                       1.0               1.0   \n",
      "32660                       0.0                       0.0               0.0   \n",
      "32661                       0.0                       0.0               0.0   \n",
      "32662                       0.0                       0.0               1.0   \n",
      "32663                      -1.0                      -1.0              -1.0   \n",
      "\n",
      "       出门4_library_count  出门3_library_count  isnull  \n",
      "id                                                   \n",
      "6                    1.0                1.0       4  \n",
      "7                   24.0               11.0       4  \n",
      "12                   0.0                0.0      14  \n",
      "13                  -1.0               -1.0      40  \n",
      "14                  -1.0               -1.0      40  \n",
      "24                  -1.0               -1.0      28  \n",
      "25                  -1.0               -1.0      44  \n",
      "26                  -1.0               -1.0      44  \n",
      "27                   0.0                0.0      14  \n",
      "30                   8.0                7.0      14  \n",
      "31                   1.0                0.0      14  \n",
      "40                   2.0                4.0       0  \n",
      "41                   4.0                0.0       0  \n",
      "42                  -1.0               -1.0      10  \n",
      "43                  -1.0               -1.0      40  \n",
      "44                  17.0                9.0       0  \n",
      "45                   1.0                0.0       0  \n",
      "50                   1.0                5.0       4  \n",
      "51                  18.0               22.0       4  \n",
      "52                   5.0                2.0       4  \n",
      "53                  19.0                6.0      18  \n",
      "54                   6.0                3.0       0  \n",
      "55                  14.0               13.0       0  \n",
      "58                  11.0               13.0       0  \n",
      "59                   0.0                2.0      14  \n",
      "68                   0.0                2.0       4  \n",
      "69                  -1.0               -1.0      44  \n",
      "78                  17.0               11.0       0  \n",
      "79                   1.0                4.0       0  \n",
      "80                  -1.0               -1.0      10  \n",
      "...                  ...                ...     ...  \n",
      "32591               -1.0               -1.0      40  \n",
      "32592               -1.0               -1.0      40  \n",
      "32593               11.0                9.0       4  \n",
      "32594               15.0                5.0       4  \n",
      "32595               31.0                0.0       0  \n",
      "32596                3.0                4.0       0  \n",
      "32597                1.0                0.0       4  \n",
      "32598               16.0                8.0      18  \n",
      "32599                4.0                4.0      18  \n",
      "32600                0.0                0.0       4  \n",
      "32601               34.0               22.0       0  \n",
      "32604               12.0                4.0       0  \n",
      "32605               11.0               15.0       4  \n",
      "32616               10.0                3.0       0  \n",
      "32617                0.0                0.0       4  \n",
      "32620               63.0               39.0       4  \n",
      "32621                0.0                0.0       0  \n",
      "32628                4.0                6.0       0  \n",
      "32629                4.0                3.0       0  \n",
      "32632               -1.0               -1.0      10  \n",
      "32633                0.0                0.0       4  \n",
      "32634                0.0                1.0       4  \n",
      "32635               -1.0               -1.0      44  \n",
      "32636               -1.0               -1.0      14  \n",
      "32637               -1.0               -1.0      10  \n",
      "32651                4.0                7.0       0  \n",
      "32660                0.0                0.0      14  \n",
      "32661                6.0               15.0       0  \n",
      "32662                9.0                8.0       4  \n",
      "32663               -1.0               -1.0      44  \n",
      "\n",
      "[10961 rows x 132 columns]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Anaconda\\lib\\site-packages\\ipykernel\\__main__.py:15: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n"
     ]
    }
   ],
   "source": [
    "#### Oversample\n",
    "del test[\"money\"]\n",
    "Oversampling1000 = train.loc[train.money == 1000]\n",
    "Oversampling1500 = train.loc[train.money == 1500]\n",
    "Oversampling2000 = train.loc[train.money == 2000]\n",
    "for i in range(8):\n",
    "    train = train.append(Oversampling1000)\n",
    "for j in range(11):\n",
    "    train = train.append(Oversampling1500)\n",
    "for k in range(12):\n",
    "    train = train.append(Oversampling2000)\n",
    "\n",
    "# model\n",
    "train['isnull'] =  train.isnull().sum(axis=1)\n",
    "test['isnull'] = test.isnull().sum(axis=1)\n",
    "train = train.set_index([\"id\"]).fillna(-1)\n",
    "test = test.set_index([\"id\"]).fillna(-1)\n",
    "train['money'] = train['money'].apply(lambda x: ftrans(x))\n",
    "print train\n",
    "print test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(9325, 133)\n",
      "(6669, 133)\n",
      "(5580, 133)\n",
      "(4602, 133)\n"
     ]
    }
   ],
   "source": [
    "print train[train.money==0].shape\n",
    "print train[train.money==1].shape\n",
    "print train[train.money==2].shape\n",
    "print train[train.money==3].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def pipeline(seed,max_depth,colsample_bytree,num_round):\n",
    "    np.random.seed(seed)\n",
    "    X_train = train.iloc[np.random.permutation(len(train))]\n",
    "    y_train = X_train.money\n",
    "    X_train = X_train.drop(['money'],axis=1)\n",
    "    \n",
    "    xgb_train   = xgb.DMatrix(X_train,y_train)\n",
    "    xgb_test = xgb.DMatrix(test)\n",
    "    \n",
    "    xgb_params = {\n",
    "        'booster': 'gbtree',\n",
    "        #'objective': 'multi:softmax',\n",
    "        'objective': 'multi:softprob',\n",
    "        'num_class': 4,\n",
    "        'max_depth': max_depth,\n",
    "        'learning_rate': 0.04,\n",
    "        'colsample_bytree': colsample_bytree,\n",
    "        'subsample': 0.8,\n",
    "        'eval_metric': 'merror',\n",
    "        'silent': 1,\n",
    "    }\n",
    "    \n",
    "    watchlist = [(xgb_train,'train')]\n",
    "    num_round= num_round\n",
    "    bst = xgb.train(xgb_params,xgb_train,num_round,watchlist,verbose_eval=False)\n",
    "    yprob = bst.predict(xgb_test).reshape( test.shape[0], 4 )\n",
    "    ylabel = np.argmax(yprob, axis=1)\n",
    "    pd.DataFrame(yprob).to_csv(\"xgb_prob_seed100_1_0.02783.csv\")\n",
    "    \n",
    "    pred = pd.DataFrame({'studentid': test.reset_index()['id'],\n",
    "                             'subsidy': ylabel})\n",
    "    pred['subsidy'] = pred['subsidy'].apply(lambda x: trans(x))\n",
    "    pred.to_csv(\"./xgb_pred_seed100_1_0.02783.csv\", index=False)\n",
    "    #print \"数据量:\", train.shape[0], \":\", pred.shape[0]\n",
    "    #print \"有奖数量:\", sum(train['money'] != 0), \":\", sum(pred['subsidy'] != 0)\n",
    "    print \"1000奖学金数量：\", sum(train['money'] == 1000), sum(pred['subsidy'] == 1000)\n",
    "    print \"1500奖学金数量：\", sum(train['money'] == 1500), sum(pred['subsidy'] == 1500)\n",
    "    print \"2000奖学金数量：\", sum(train['money'] == 2000), sum(pred['subsidy'] == 2000)\n",
    "    pd.Series(bst.get_fscore()).reset_index().sort_values(0).to_csv(\"feature_xgb_prob_seed100_1_0.02783.csv\",encoding=\"GB18030\")\n",
    "    return yprob,sum(pred['subsidy'] == 1000),sum(pred['subsidy'] == 1500),sum(pred['subsidy'] == 2000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 执行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000奖学金数量： 0 2516\n",
      "1500奖学金数量： 0 775\n",
      "2000奖学金数量： 0 279\n"
     ]
    }
   ],
   "source": [
    "prob,a,b,c = pipeline(100,5,0.8,100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "pd.DataFrame(prob).to_csv(\"xgb_prob_seed100_1_0.02783.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.26054969  0.34977007  0.1663022   0.22337808]\n",
      " [ 0.51253694  0.17336488  0.21597905  0.09811909]\n",
      " [ 0.65440506  0.18068506  0.09521852  0.06969137]\n",
      " ..., \n",
      " [ 0.40733448  0.24945877  0.26073369  0.08247302]\n",
      " [ 0.96712822  0.0118625   0.0086104   0.01239888]\n",
      " [ 0.88091147  0.05166671  0.02918606  0.03823573]]\n"
     ]
    }
   ],
   "source": [
    "print a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'bst' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-51-c5df2187c8ab>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSeries\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbst\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_fscore\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreset_index\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort_values\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mto_csv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"feature_xgb_prob_seed100_1_0.02783.csv\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mencoding\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"GB18030\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'bst' is not defined"
     ]
    }
   ],
   "source": [
    "pd.Series(bst.get_fscore()).reset_index().sort_values(0).to_csv(\"feature_xgb_prob_seed100_1_0.02783.csv\",encoding=\"GB18030\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "-----------------------------------------\n",
    "1000,5,0.8,100\n",
    "数据量: 26176 : 10783\n",
    "有奖数量: 16851 : 3402\n",
    "1000奖学金数量： 0 2408\n",
    "1500奖学金数量： 0 721\n",
    "2000奖学金数量： 0 273\n",
    "0.03022\n",
    "----------------------------------------\n",
    "1500,5,0.8,100\n",
    "数据量: 26176 : 10783\n",
    "有奖数量: 16851 : 3390\n",
    "1000奖学金数量： 0 2409\n",
    "1500奖学金数量： 0 718\n",
    "2000奖学金数量： 0 263\n",
    "0.02***\n",
    "----------------------------------------\n",
    "999,5,0.8,100\n",
    "数据量: 26176 : 10783\n",
    "有奖数量: 16851 : 3357\n",
    "1000奖学金数量： 0 2364\n",
    "1500奖学金数量： 0 724\n",
    "2000奖学金数量： 0 269\n",
    "0.2***\n",
    "----------------------------------------\n",
    "4000,5,0.8,100\n",
    "数据量: 26176 : 10783\n",
    "有奖数量: 16851 : 3369\n",
    "1000奖学金数量： 0 2416\n",
    "1500奖学金数量： 0 712\n",
    "2000奖学金数量： 0 241\n",
    "0.03031\n",
    "-------------------------------------\n",
    "\n",
    "model4 \n",
    "---------------------------------------------------------------------\n",
    "---------------------------------------------------------------------\n",
    "2432 680 263\n",
    "-----------------------------\n",
    "seed = 100,5,0.8,100\n",
    "1000奖学金数量： 0 2436\n",
    "1500奖学金数量： 0 678\n",
    "2000奖学金数量： 0 265\n",
    "(2436, 678, 265)\n",
    "0.03041\n",
    "---------------------------\n",
    "seed =3480\n",
    "1000奖学金数量： 0 2434\n",
    "1500奖学金数量： 0 681\n",
    "2000奖学金数量： 0 272\n",
    "(2434, 681, 272)\n",
    "不到0.03\n",
    "在另外两个差别不大的情况下，包括两种情况，一是数和分布都一样，那么2000人数增加导致分数下降；另一种是分布不一致，这个要另外考虑了。\n",
    "--------------------------\n",
    "1560\n",
    "数据量: 26176 : 10783\n",
    "有奖数量: 16851 : 3372\n",
    "1000奖学金数量： 0 2412\n",
    "1500奖学金数量： 0 674\n",
    "2000奖学金数量： 0 286\n",
    "不到0.03\n",
    "-----------------------------\n",
    "2440"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
